简体   繁体   English

如何使用nodejs中的service in function在mongodb中将boolean变量设置为true

[英]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.

相关问题 如何使用 nodejs 在 MongoDB 中将布尔属性设置为 true 或 false - How can I set Boolean property to true or false in MongoDB with nodejs 如何在lambda函数中使用nodejs将mongodb输出存储在变量中? - How to store mongodb output in variable using nodejs in lambda function? 如何使用nodejs发起mongodb副本集 - How to initiate mongodb replica set using nodejs 未在函数nodejs中设置变量 - Variable not set in function nodejs Nodejs + mongodb +动态$ set无需声明变量 - Nodejs + mongodb + dynamic $set without declaring a variable NodeJS/MongoDB:仅当变量为真时才想有条件地进行查找 - NodeJS/MongoDB : Wanna condiotinally make a lookup only if a variable is true Node.js:将请求模块用于MongoDB时如何设置限制 - Nodejs: How to set limit when using Request module for mongodb 如何在nodejs mongodb驱动程序中使用mongoclient设置read Preference secondary? - how to set read Preference secondary using mongoclient in nodejs mongodb driver? 如何使用猫鼬,nodejs和reactjs在mongodb中修复``设置true / false / value而不是null&#39;&#39; - How to fix 'setting true/false/value instead of null in mongodb using mongoose, nodejs and reactjs' Node.js:未定义的变量如何评估为true? - Nodejs: how can undefined variable evaluate to true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM