findOneAndUpdate is not a function - Error

2,090 views
Skip to first unread message

Vladimir Ilic

unread,
May 4, 2016, 12:56:36 PM5/4/16
to Keystone JS
Hello,
I want to update existing document or insert new one if it does not exist using findOneAndUpdate mongoose function, but I receive error:
newDocument.findOneAndUpdate(
              ^

TypeError: newDocument.findOneAndUpdate is not a function


Here is extract of my code:
 var keystone = require('keystone'),
     async = require('async'),
     KModel = keystone.list('KPosts');
 
 function createContent(document, done) {
var newDocument = new KModel.model();
  newDocument.findOneAndUpdate(
    { "_id": document._id},  // query
    { $setOnInsert: document}, // update or insert if does not exist
    {
      new: true,   // return new doc if one is upserted
      upsert: true // insert the document if it does not exist
    },
    function(err, object) {
      if (err){
       console.warn(err.message);  // returns error if no matching object found
      }
     done(err);
    });
 }

 

Since findOneAndUpdate is mongoose function I expected I can use it in keystone project.
Any suggestion how to use findOneAndUpdate?

thanks,
Vladimir

Vladimir Ilic

unread,
May 5, 2016, 11:06:45 AM5/5/16
to Keystone JS
Ok, I managed to get this work.
here is code that do the job.
keystone.list('Enquiry').model.findOneAndUpdate(
    { '_id': document._id},  // query

Ryan Cole

unread,
Jun 15, 2016, 5:03:13 AM6/15/16
to Keystone JS
Right I think the .model. just exposes the mongoose model. 
Reply all
Reply to author
Forward
0 new messages