Node.js mongodb:无法将属性设置为null,尽管它在shell中工作
我正在尝试用 Nodejs,mongodb,Nexmo 但它在壳牌公司工作 这是我的密码 电话SHCHEMANode.js mongodb:无法将属性设置为null,尽管它在shell中工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试用 Nodejs,mongodb,Nexmo 但它在壳牌公司工作 这是我的密码 电话SHCHEMA const phoneSchema = new Schema({ mobileNo:{type: String}, count:{type:String}, verified:{ type:String, defaultValue:0 }, lastFive:{ type:Date,
const phoneSchema = new Schema({
mobileNo:{type: String},
count:{type:String},
verified:{
type:String,
defaultValue:0
},
lastFive:{
type:Date,
default: Date.now
},
},{ timestamps: true})
const Phone = mongoose.model('Phone', phoneSchema)
module.exports = Phone
const otpSchema = new Schema({
otp:{
type:Number,
unique: true
},
mobileNo:{ type:String,}
},{ timestamps: true})
const Otp = mongoose.model('Otp', otpSchema)
module.exports = Otp
OTP模式
const phoneSchema = new Schema({
mobileNo:{type: String},
count:{type:String},
verified:{
type:String,
defaultValue:0
},
lastFive:{
type:Date,
default: Date.now
},
},{ timestamps: true})
const Phone = mongoose.model('Phone', phoneSchema)
module.exports = Phone
const otpSchema = new Schema({
otp:{
type:Number,
unique: true
},
mobileNo:{ type:String,}
},{ timestamps: true})
const Otp = mongoose.model('Otp', otpSchema)
module.exports = Otp
登录控制器
const nexmo = new Nexmo({
apiKey: '#######',
apiSecret: '########'
})
exports.getOtp = async (req,res,next)=>{
try{
const {mobileNo} = req.body
let phone = await MobileNo.findOne({mobileNo})
if(!phone){
const error = new Error('Number you have entered is not registered Please contact dealer')
throw error
}
else{
Otp.findOneAndDelete({mobileNo})
const dates= phone.lastFive.toString().split(';')
if(dates.length>=5){
const msec=(Date.now()- parseInt(dates[0]) )
const minute=Math.floor(msec / 1000 / 60)
if(minute<=5){
return res.json({
err:'5 minute 5 Otp limit. Wait'
})
}else{
phone.count++;
phone.lastFive=Date.now();
}}
else{
phone.count++;
phone.lastFive=`${phone.lastFive} ${Date.now()}`
}}
let otp,randomOtp;
while(true){
randomOtp = Math.floor( Math.random() * (10000 - 1000) + 1000)
otp = await Otp.findOne({otp} )
if(!otp){
otp = new Otp({
otp:randomOtp,
mobileNo
})
setTimeout(()=>{
otp.delete();
},1000*60*5);
break}}
const from = 'Nexmo'
const to = mobileNo
const text = `Verification code :- ${randomOtp}`
nexmo.message.sendSms(from, to, text,async (err, response) => {
if (err) {
next(err);
} else {
await phone.save()
await otp.save()
res.json({
msg:'OTP is send Successfuly',
success:true
})
}
})}
catch(err){
next(err)}}
VERIFYING OTP
exports.verifyOtp= async(req,res,next)=>{
const {otp,mobileNo}= req.body;
try{
const checkOtp = await Otp.findOne({otp,mobileNo});
if(!checkOtp){
throw new Error('Invalid Otp')
}
else
{
const findPhone = await MobileNo.findOne({ mobileNo:checkOtp.mobileNo,otp} )
findPhone.verified=1;
findPhone.save();
checkOtp.delete();
return res.json({
msg:'OTP is verified',
success:true
})}}
catch(err){
next(err)}}
**错误**
findPhone null TypeError:无法设置null的属性“已验证”
在exports.verifyOtp(#######\backend\src\controllers\loginController.js:164:31)
在处理和拒绝时(内部/process/task_queues.js:93:5)
您好,看起来您的行
const findPhone=wait MobileNo.findOne({MobileNo:checkOtp.MobileNo,otp})
正在返回null,然后您尝试在null值内修改字段verified
,这是不可能的。谢谢Theo,我已经删除了“otp”参数,它工作正常