1

I don't think this code is finding.

So I'm not getting the update or the insert and I'm also not getting any errors. No error compiling or run-time.

///////// Sfy.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var SfySchema = new Schema({
        f1: { type: String, required: true }
        , f2: { type: String, required: true }
        , f3: {type: Object, required: true }
    }, {
        timestamps: true
    }
);
var mdlSfy = mongoose.model('Sfy', SfySchema);

module.exports = mdlSfy;



//////////file 2
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var Sfy = requires('Sfy');

var dbConnecString = "bla bla bla";
mongoose.connect(dbConnectionString, {}, function (err) { ... });

var n = 'name';
var c = 'country';
var o = { f2: c, f3: { ....inner object.... } };

var dat = { f1: n, f2: o.f2, f3: o };

Sfy.findOneAndUpdate({ f1: dat.f1, f2: dat.f2 }, dat, { upsert: true }, function (err, doc) {
    if (err)
        console.log('error upserting', err);
});
Steve
  • 905
  • 1
  • 8
  • 32
  • How are you verifying that you're not getting the update or insert? – JohnnyHK Apr 15 '17 at 17:33
  • @JohnnyHK robomongo – Steve Apr 15 '17 at 18:24
  • But thinking about it. If the find is failing then I should be getting an insert. So what else could be wrong? – Steve Apr 15 '17 at 18:25
  • You may be looking in the wrong collection. See https://stackoverflow.com/questions/14183611/mongoose-always-returning-an-empty-array-nodejs – JohnnyHK Apr 15 '17 at 18:27
  • @JohnnyHK Actually this is my very first use of this schema. Therefore mongoose will create the schema anew as soon as I want to persist a document. By my code it should as it has for me in other schema, create a `Sfys` collection – Steve Apr 15 '17 at 18:34
  • I wonder how I can get mongod logging to help me see what it sees mongoose doing – Steve Apr 15 '17 at 18:35
  • Add a call to `mongoose.set('debug', true);` to see what mongoose is doing. – JohnnyHK Apr 15 '17 at 18:37
  • That actually didn't do anything. no idea why. But i figured out I'm loosing my connection to mongod somewhere between creating the connection and findOneAndUpdate() – Steve Apr 15 '17 at 19:01
  • @JohnnyHK a case sensitive typo in the name of the variable I was passing to mongoose.connect() for the connection string. Mean I was passing something else as a connection string than what i thought I was. – Steve Apr 15 '17 at 19:18

0 Answers0