单击按钮时将id设置为true时如何保存其他详细信息

时间:2019-04-01 06:46:56

标签: node.js angular6

我无法正确保存变量,返回的布尔变量在单击按钮时设置为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);
                       });     
                    });    

1 个答案:

答案 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);
                            }
                         });