0
您好我想連接到mongo數據庫來檢查用戶是否已經註冊或如果不是,我想保存用戶phone_number併爲他的登錄做一個代碼,如果他已經註冊我想只是發送代碼登錄 的問題是,當我寫的代碼不檢查(如果)語句,並繼續到另一端,發送空的錯誤從.then連接到mongodb
userModel.findOne({phone_number: req.body.phone_number}).then(function (err, res) {
console.log('if start');
if (err) {
res.send(err)
} else {
if (res.length){
codeModel.create({
phone_number: req.body.phone_number,
code_Number: Math.floor((Math.random() * 5000) + 1000)
}).then(function (created) {
console.log('response then');
res.status(200).send(created);
})
}
else {
console.log("blah blah blah");
userModel.create(req.body).then(function (jobs) {
res.status(200).send(jobs)
})
}
}
['然後()'](https://developer.mozi lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)沒有'(err,res)'它只有'res'。 ''.catch()'。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch)處理'err'。同樣通過你的「模型」命名和'.create()'方法,這將是「貓鼬」。 –
是的,它是貓鼬所以如果我修正你說我的代碼應該罰款? –
你應該閱讀這些鏈接。承諾與回調不同。邏輯中還存在延續問題。但一次只能一步。 –