mongoose.save()有两种types的行为

我正在学习mongoose,我正在做一个简单的发布请求来添加一个用户到我的mongolabtesting数据库。 我正在使用一个基本的用户架构,但是当我运行save()方法时,我有时会得到一个

未处理的承诺拒绝(拒绝ID:1):错误:需要数据和salt参数

有时甚至没有任何反应,应用程序根本就什么也不做 我正在使用邮递员来testing发布请求。

编辑 :由于mikeybuild议我删除Resolve和Rejectcallback,并处理.save()callback内的一切,但现在我得到以下错误:

(node:10964)DeprecationWarning:Mongoose:mpromise(mongoose的默认承诺库)已被弃用,插入自己的承诺库,而不是:http:// mongoosejs.com/docs/promises.html

var express = require('express'); var morgan = require('morgan'); var mongoose = require('mongoose'); var bodyParser = require("body-parser"); var mPromise = require("mpromise"); var User = require('./models/user'); var app = express(); mongoose.connect('mongodb://root2:1234@ds161742.mlab.com:61742/ecommerce', function (err) { if (err) console.log(err); console.log("Connected to the database"); }); app.use(morgan('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.post("/create-user", function (req, res, next) { var user = new User(); user.profile.name = req.body.name; user.password = req.body.password; user.email = req.body.email; user.save(function (err, user) { if (err) { res.send("deu erro"); } else { console.log("Deu bom"); res.send("deu bom"); } }) }) app.get("/get-users", function (req, res, next) { User.find({}) .exec(function (err, users) { if (err) res.send("Erro na hora de pegar os usuarios " + err); res.send(users); }); }); app.get('/', function (req, res) { res.send("Deu mais bom"); }); app.listen(80, function (err) { if (err) throw err; console.log("Server is running on port 80"); }); 

另外当我连接到mongolab我得到的警告:

DeprecationWarning: open()在mongoose> = 4.11.0中不推荐使用,而是使用openUri() ,或者在使用conn ect()createConnection()设置useMongoClient选项。 请参阅http://mongoosejs.com/docs/connections.html#use-mongo-client服务器正在端口80上运行Db.prototype.authenticate方法将不再在下一个主要版本3.x中可用,因为MongoDB 3.6将只允许对pipe理数据库中的用户进行身份validation,并且不再允许套接字上的多个凭据。 请使用具有授权凭证的MongoClient.connect进行validation。

但我没有在我的代码中使用任何open()方法,因为我没有使用默认的mongoDB库。 我能够将一个集合添加到mongolab数据库,但数据不完整,现在我正在努力。

编辑2 :这是我使用bcrypt的UserSchema的代码:

 var mongoose = require('mongoose'); var bcrypt = require('bcrypt'); var Schema = mongoose.Schema; /* The user schema attributes/fields */ var UserSchema = new Schema ({ email : String, password: String, profile: { name: {type: String, default: "Sem nome"}, picture: {type: String, default: ''} }, address: String, history: [{ date: Date, paid: {type: Number, default: 0}, //item: { type: Schema.Types.ObjectId, ref: ''} }] }); /* The method to hash the password before saving it to the database */ UserSchema.pre('save', function(next){ var user = this; if(!user.isModified('password')) return next(); bcrypt.genSalt(10, function(err, salt){ if(err) return next(err); bcrypt.hash(user.password, salt, null, function(err, hash){ if(err) return next(err); user.password = hash; next(); }); }); }); /* Compare the password between the database and the input from the user */ UserSchema.methods.comparePasswords = function(inputpassword){ return bcrypt.compareSync(inputpassword, this.password); } module.exports = mongoose.model('User', UserSchema); 

任何帮助表示赞赏,谢谢

除非我错了, bcrypt.hash()接受3个参数不是4.因此,它的callback可能永远不会被解雇。

应该很简单

 bcrypt.hash(user.password, salt, function (err, hash){ if(err) return next(err); user.password = hash; next(); }); 

您可能还想检查user.passwordsalt是否已定义 。

要解决你的第一个警告,关于mpromise ,你可以使用原生的Promise (Node version> = 6):

 mongoose.Promise = global.Promise; 

要解决你的第二个警告,你必须使用useMongoClientdocumentation提供了一个承诺的方法:

 function connectDatabase(databaseUri) { var promise = mongoose.connect(databaseUri, { useMongoClient: true, }); return promise; } connectDatabase('mongodb://root2:1234@ds161742.mlab.com:61742/ecommerce') .then(() => console.log("Connected to the database");) .catch((err) => console.log(err)); 

下面的代码可以解决你所有的问题

 var mongoose = require('mongoose'); var options = { useMongoClient:true }; var dbUrl = 'mongodb://root2:1234@ds161742.mlab.com:61742/ecommerce'; mongoose.connect(dbUrl,options); mongoose.Promise = global.Promise;