Saturday 15 April 2017

node.js mongoose findOneAndUpdate()

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);
});



via user2367083

No comments:

Post a Comment