我无法正确保存变量,返回的布尔变量在单击按钮时设置为true。
// api.js
router.get('/return/:id?',(req,res)=>{
var id=req.params.id;
console.log(id);
bookissue.update({_id:id},{$set:{"isreturned":true}},(error,result)=>
{
if(error){
console.log(error);
}
console.log(bookissue.status);
var date1 = new Date();
console.log(date1);
//Another error is that bookissue.returndate is returning an invalid date message.
var date2 = bookissue.returndate;
console.log(bookissue.returndate);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(diffDays);
if (diffDays !== 0) {
const res = 50;
this.fine = res + diffDays;
}
else {this.fine = 0; }
bookissue.fine=this.fine;
console.log(bookissue.fine);
});
});
答案 0 :(得分:0)
更新bookIssue属性时,需要再次保存/更新对象。
router.get('/return/:id?',(req,res)=>{
var id=req.params.id;
console.log(id);
bookissue.update({_id:id},{$set:{"isreturned":true}},(error,result)=>
{
if(error){
console.log(error);
}
console.log(bookissue.status);
var date1 = new Date();
console.log(date1);
//Another error is that bookissue.returndate is returning an invalid date message.
var date2 = bookissue.returndate;
console.log(bookissue.returndate);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(diffDays);
if (diffDays !== 0) {
const res = 50;
this.fine = res + diffDays;
}
else {this.fine = 0; }
bookissue.fine=this.fine;
console.log(bookissue.fine);
bookissue.save((err, updatedBookIssue) => {
if (err) {
console.log(err);
} else {
console.log('Updated book issue');
console.log(updatedBookIssue);
}
});
});
});
检查上面的代码。
分配了fine
变量后,我再次保存该对象。
我在代码末尾添加了以下代码
bookissue.save((err, updatedBookIssue) => {
if (err) {
console.log(err);
} else {
console.log('Updated book issue');
console.log(updatedBookIssue);
}
});