Reputation: 6039
Consider a mongodb collection running on MongooseJS.
Person.where('uid').equals(19524121).select('name').exec(function(err, data){
// Here I can get the data correctly in an array.
console.log(JSON.stringify(data));
data[0].name = "try to save me now"; // Select the first item in the array
data[0].save(); // Object #<Promise> has no method 'save'.
}
Object #<Promise> has no method 'save';
I am a little confused on why this is happening and I have researched quite a bit and can't seem to find a direct answer for this.
Upvotes: 3
Views: 4400
Reputation: 7427
The result of a find
is an array of records. You probably meant to loop over those records like this:
Person.find({ uid: /19524121/ }).select('name').exec(function(err, data){
for(var i = 0; i < data.length; i++) {
var myData = new Person(data[i]);
myData.name = "try to save me now";
myData.save(); // It works now!
}
}
Also, from the mongoose homepage, it appears that the function callback prototype is function(err, data)
, not the other way around, which you corrected above.
Look at this from the homepage:
var fluffy = new Kitten({ name: 'fluffy' });
If data[0]
currently has a regular JSON object, we need a line like this to convert to a BSON model object.
var myData = new Person(data[0]);
Upvotes: 12