Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose instance .save() not working

I have a problem with Mongoose and MongoDb

It is very interesting that only Model.update works and save never works and does not even fire callback.

Mongoose: 4.4.5 MongoDB: 3.0.8

Express Route

var mongoose = require('mongoose'); mongoose.connect("mongodb://127.0.0.1:27017/db"); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function(callback) {     console.log("connection to db open") }); var User = require("../models/user.js"); 

User Model

var user = new Schema({     uid: { type: Number, required: true, unique: true},     hwid: { type: String, default:""},     bol:{type:String,default:""} }); 

Update Enpoint

Working version: Model.update()

User.update({_id: id}, {     uid: 5,  }, function(err, numberAffected, rawResponse) {     console.log(err); }) 

Not working version and I have to solve this: Object.save()

User.find({_id:id}, function(err,user){     if(err){          console.log(err);     }     if(!user){          console.log("No user");     }else{         user.uid = 5;         user.save(function(err,news){             console.log("Tried to save...");         });     }     console.log("At least worked"); }) 

Even callback is not firing. Connection successfully opens. It never invokes callback.


  1. Tried to use var User = connection.model('User', schema) didn't work.
like image 453
Ahmet Can Güven Avatar asked Mar 01 '16 21:03

Ahmet Can Güven


People also ask

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

When I do a post the data is not saved in MongoDB only _ID is saved?

It means that Mongoose is looking for a value with the key of "any", literally. If your request body does not contain such key (and it does not, as it only contains name and age), it will not be picked up when you save the new object. What you are experiencing is the expected behaviour of Mongoose.

Does update call save in Mongoose?

save() you already have an object in your client side code or had to retrieve the data from the server before you are writing it back, and you are writing back the whole thing. On the other hand . update() does not require the data to be loaded to the client from the server.

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. Bookmark this question.


1 Answers

I have the same problem. my problem was about changing an array inside db, then when I try to use .save(), it didn't understand that I changed any thing, then the .save() didn't work. I just use markModified() before use .save() and my problem become solved.

this is my code with problem: (not working)

club.members[index].name = new_name; club.save(); 

this is my solved code: (working)

club.members[index].name = new_name; club.markModified('members'); club.save(); 

enjoy!

like image 51
Abolfazl Miadian Avatar answered Sep 16 '22 11:09

Abolfazl Miadian