Mongoose.save () instance not working

I have a problem with Mongoose and MongoDb

It is very interesting that only Model.update , and save never works or even starts a 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); }) 

The version does not work, and I have to solve this problem: 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 the callback does not work. The connection opens successfully. It never calls a callback.


  • Tried to use var User = connection.model('User', schema) did not work.
+11
source share
6 answers

I am not going to delete this question because people may run into this problem too. Actually the problem was not related to MongoDb or Mongoose. When you call Object.save() chain of responsibility is as follows:

  1. Schema.pre ("save")
  2. Save Data to Dabe
  3. Schema.post ("save")

Therefore, if you block pre("save") and do not call the next() handler, you cannot save your document. This was my case, I forgot the next() call inside the if statement and tried to find the error for more than 3 hours.

 user.pre("save", function(next) { if(!this.trial){ //do your job here next(); } } 

When this.trial == true , the next handler will not be available.

To avoid such errors, we must take care of covering the branch, reporters can show us unverified codes. Your problem may be related to this too. Make sure you call next() if your document needs to be saved.

Fixed version

 user.pre("save", function(next) { if(!this.trial){ //do your job here } next(); } 
+18
source

As Paul said. Most likely, you call save on the req.user object, which is not a Mongoose object. Make sure you do something like this:

 //I am using your 'user' schema var userModel = mongoose.model('User', user); var User = mongoose.model('User'); var newUser = new User(req.user); newUser.save(function(error, user){ //your code } 
+2
source

It sounds nuts .. and I tried to work through this problem for hours. Looked at so many columns .. this is incredible.

And you know what that was? I did not indicate the database at the end of the URL.

So instead

 "mongodb://127.0.0.1:27017/test" 

I had

 "mongodb://127.0.0.1:27017 

I spent the whole day on this. I am very sorry that they did not give me any mistakes. Saving a record always returned normally. And in the database log I connected normally. But I really needed to look at the details. Yes, it connected to the mongo instance, but not to the database itself. pah!

+2
source

Just in case, this happens to someone else.

Another reason may be that you do not have an open connection to the mongodb instance. Check the output for appropriate feedback.

[initandlisten] connection accepted from 127.0.0.1:40448 # 1 (1 connection is now open)

0
source

I ran into the same problem, it turns out that instance.save() returns a promise. So all you have to do is keep your promise. Using async/await -

 await instance.save() 
0
source

I have the same problem. My problem was changing the array inside the database, then when I tried to use .save (), it did not understand that I changed something, then .save () did not work. I just use markModified () before using .save () and my problem is solved.

this is my code with the problem: (doesn't work)

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

this is my solved code: (works)

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

enjoy!

0
source

Source: https://habr.com/ru/post/1244255/


All Articles