why mongoose queries dos not work when put inside promise function

前端 未结 1 730
时光取名叫无心
时光取名叫无心 2021-01-27 20:29

My code is as shown below:

checkAndCreateUser(customer_id, email_id).then(result => {
        console.log(\"result is \" + result);
    });

var checkAndCreat         


        
1条回答
  •  不思量自难忘°
    2021-01-27 21:19

    You forgot save your user, besides Mongoose already returned promise, you don't need use callbacks:

    var checkAndCreateUser = function (custom_id, email) {
        return User.create({ ph_no :ph_no,
                             email_id :email})
        .then(result=>{
            return User.findOne({'email_id': email})
        })
        .then(user=>{
           return user._id;
        })
    };
    

    As for mistake in your function:

    ...
    let user = new User({email_id: email, ph_no: phone});
    user.save();//you forgot about this
    ...
    

    You can use save() with callback:

    user.save((err, result)=>{...});
    

    Or with promise:

     user.save().then(result=>{...}).catch(err=>{...});
    

    0 讨论(0)
提交回复
热议问题