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.