I'm learning mongoose and I'm making a simple post request to add a user to my mongolab test database. I am using a basic user schema but when I run the save() method I sometimes get an
Unhandled promise rejection (rejection id: 1): Error: data and salt arguments required
and sometimes nothing happens, the app simply does nothing. I'm using Postman to test the post request.
Edit: As mikey suggested I removed the Resolve and Reject callbacks and treated everything inside the .save() callback, but now I get the following error:
(node:10964) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: 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");
});
Also when I connect to mongolab I get the warning:
DeprecationWarning:
open()
is deprecated in mongoose >= 4.11.0, useopenUri()
instead, or set theuseMongoClient
option if usingconn ect()
orcreateConnection()
. See http://mongoosejs.com/docs/connections.html#use-mongo-client Server is running on port 80 Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
but I'm not using any open() methods in my code since I'm not using the default mongoDB library. I was able to add a collection to the mongolab database but the data was incomplete, and now I'm struggling with this.
Edit2: Here is the code of my UserSchema that uses bcrypt:
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);
Any help is appreciated, thanks