登录用户如何编辑他的属性?

时间:2019-07-13 12:54:31

标签: node.js express mongoose middleware

我已经在猫鼬中创建了一个架构

{"_id":"5d035531f2f68b342c496584","name":"Raj","email":"r@r.com","password":"$2a$10$Je4oAWhwtch0D4CDl39WyOCVRVVSYW8Fp7e5OnNa2qFl8Ffj86cZe","location":"Raniganj","createdDate":"2019-06-14T08:05:05.525Z","farm":[{"_id":"5d1da0894802882a4c37cda2","farmAddDate":"2019-07-04T06:45:29.669Z","device":[{"_id":"5d2329feca87a31728be8662","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},{"_id":"5d232f7c0262ed46dcf6ba21","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},null,{"_id":"5d29bc258e4e1504cc9985ca","post":"HI"},{"_id":"5d29bc458e4e1504cc9985cc","post":"HI"},{"_id":"5d29bcd38e4e1504cc9985ce","post":"HI"},{"_id":"5d29bd64dc9ad52bf861ff5b","post":"HI2"},{"_id":"5d29c7668d583d561801bd43","post":"HI2"},{"_id":"5d29c783e0ca0a34bc0ed1be","post":"HI2"},{"_id":"5d29cbd2126e2d51e826b701","post":"HI2"}]},{"_id":"5d1de7017bae663944431197","farmAddDate":"2019-07-04T11:46:09.692Z","device":[{"_id":"5d2329feca87a31728be8662","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},{"_id":"5d2352b9cd32021100998d45","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},{"_id":"5d249b837be8eb23f8fb9511","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},null,null,{"_id":"5d2998246846e8575870765a"},{"_id":"5d2998c2549ed044c493026f"},{"_id":"5d29993134fe83408008a99f"},{"_id":"5d299fdd7b039719c4362593","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"}]},{"_id":"5d1efa88e1627f1fb05d95ac","farmAddDate":"2019-07-05T07:21:44.668Z","device":[{"_id":"5d2329feca87a31728be8662","modelNumber":"xxx-xxx-sp01","deviceType":"Rasberry","description":"Just testing Manual Data input from Request Body","deviceLocaion":"12.30"},null]}],"__v":0,"farmAddDate":"6709379463479558145","deviceModifyDate":"6713072491109023745","farmModifyDate":"6710075956851113985","lastModified":"2019-07-13T09:09:49.859Z"}

这样,我想在他登录并执行Crud时编辑他的设备对象 我该如何实现仅针对登录用户编辑此架构

我已经制作了用于添加设备的api,我不知道这对执行所需的操作会有所帮助

router.post("/Dev4/:id",(req,res) =>{
  //addDevice working on all ids
  //device are addded in farms 0
  // const id=req.params.id;
  //user_id=mongoose.Types.ObjectId(user_id);
  req.body={post:"HI2"};   //how can i request multiple user input?
   User.findByIdAndUpdate(req.params.id,
    {$push:{"farm.0.device":req.body}},
         {safe:true,upsert:false,multi:false,new:true},
          function(err,User){
      if(err)
    {

      console.log(err);
      return res.send(err);
    }
    return res.status(200).json(User);
    });
  });


0 个答案:

没有答案