$ push没有在零索引处插入值

时间:2019-07-17 05:54:07

标签: node.js mongodb mongoose

我正在使用$ push在数组中插入值,并且它在'1'索引处插入了一个值,并且每次将'零'索引留为空白。像这样:(0:'',1:value,2:value,...)

下面是我的代码,请检查并帮助我。谢谢!!

 router.post('/postToAddToProject',async (req,res)=>{

        let response;
        try {
          let dbRespo;

          //first check here if a role is a reporting manager or not 
          if(req.body.role=='Reporting Manager'){

            dbRespo=await ADMIN_PROJECTS.findOneAndUpdate({_id:req.body.id},
              {$push : {projectManager:req.body.email}} //$push to insert value at array in mongoDB
            );

            console.log(dbRespo);
          }else{
            dbRespo=await ADMIN_PROJECTS.findOneAndUpdate({_id:req.body.id}
              ,{$push: {teamMembers: req.body.email}}
            );
          }

          if(dbRespo!=null){
            response={serverMessage:1};
          }
          else{
            response={serverMessage:0};
          }


        } catch (e) {
          console.error('Error at inserting new user to project' +e);
        } finally {
          res.send(response);
        }
      });

0 个答案:

没有答案