[英]How to set boolean variable to true in mongodb using function in service in nodejs
I have created a function in service using nodejs to set the boolean variable to true upon button click. 我已经使用nodejs在服务中创建了一个函数,在单击按钮时将boolean变量设置为true。 But its not working.
但它不起作用。
I'm using get method in service to do the above. 我在服务中使用get方法来执行上述操作。 // in html
//在html中
<tr *ngFor="let status of bookstatus">
<td>{{status.bookname}}</td>
<td>{{status.issuedate | date:'dd/MM/yyyy'}}</td>
<td>{{status.returndate}}</td>
<td>{{status.membername}}</td>
<td>{{status.fine}}</td>
<td>{{status.status}}</td>
<td><button type="button" (click)="returnbook(status._id)">BookReturn</button></td>
</tr>
// in ts //在ts
returnbook(id: number) {
this.bookissueservice.returnbook(id).subscribe(response => {window.alert('book returned'); });
if (this.bookstatus.isreturned === true) {
this.bookstatus.status = 'returned';
var date1 = new Date(this.bookreturn);
var date2 = new Date(this.bookstatus.returndate);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if (diffDays !== 0) {
const res = 50;
this.fine = res + diffDays;
}
else {this.fine = 0; }
}
}
//in service //在服务中
returnbook(id:number){
return this.http.get(`http://localhost:4000/api/memberdetails/${id}`).
pipe(map(response=>response));
}
//in api //在api中
router.get('/return/:id?',(req,res)=>{
var body=req.params.id;
var id=body.id;
bookissue.findById(id,(error,bookissue)=>{if(error){
console.log(error);}
/*bookissue.isreturned is the boolean variable which I'm trying to set to true on button click */
bookissue.isreturned = 1;
bookissue.save(function(error,returned){
if(error){
console.log(error);
return next(error);
}
res.json({status:true,message:'saved',data:returned});
});
});
});
If you wanna save the boolean variable to "1" use the update query. 如果要将布尔变量保存为“1”,请使用更新查询。
bookissue.update({_id:id},{$set:{isreturned:true}},(error,result)=>{
if(error){
console.log(error);
return next(error);
}
console.log("result : ",result);
res.json({status:true,message:'saved',data:result});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.