I use passport to authenticate and for sign up when I use html form there is no problem but when I try to generate a random number and put the value in passport method although the user create in data base but doesn't work!? here is my code:
router.post("/register", function (req, res) {
var password = Math.floor((Math.random() * 100000) + 1).toString();
User.findOne().sort({
field: 'asc',
_id: -1
}).limit(1).exec(function (err, lastQ) {
if (err) {
console.log(err);
} else {
console.log(lastQ);
var customersNo = Number(lastQ.customerNo) + 1;
User.register(new User({
username: username,
customerNo: customersNo,
customerName: req.body.customerName,
customerAdd: req.body.customerAdd,
customerPhone: req.body.customerPhone
}), req.body.password, function (err, user) { ....//here works!
in part above everything OK but if change req.body.password with my variable 'password' doesn't work:
router.post("/register", function (req, res) {
var password = Math.floor((Math.random() * 100000) + 1).toString();
User.findOne().sort({
field: 'asc',
_id: -1
}).limit(1).exec(function (err, lastQ) {
if (err) {
console.log(err);
} else {
console.log(lastQ);
var customersNo = Number(lastQ.customerNo) + 1;
User.register(new User({
username: username,
customerNo: customersNo,
customerName: req.body.customerName,
customerAdd: req.body.customerAdd,
customerPhone: req.body.customerPhone
}), password, function (err, user) { ....//here doesn't work!
What error it gives? By viewing I figured out you might missing on the scope of this.
my assumption, that your password has not been generated yet is already used, maybe you can handle it with the async function,
or to be sure, try console the password, whether the password has been generated or not.