如果在 node.js 中满足条件后块仍未执行

If block is not executing even after condition is satisfied in node.js

exports.deletePost = (req, res) => {
 const post = req.post;
 let postId = req.post._id;
 let userType = req.post.userType;
 let userId = _.trim(req.post.user._id, 'new ObjectId(")');

 post.remove((err, post) => {
      if(err)
           return res.status(400).json({error:"Failed to delete the post"});
      //res.json({message:"Post deleted successfully"});
      console.log("post deleted!");
 });

 if (userType=="User") {
      User.findById(userId).exec((err, user) => {
           if(err)
                return res.status(400).json({error:err})
           else if(!user)
                return res.status(400).json({error:"User not found!"})
           else {
                let removeIndex = 0;
                user.posts.map((post, index) => {
                     console.log(post+"======="+postId); //its matching for specified postId but if block it not executed
                     if (post === postId) {
                          console.log("found the post!!!!!!!!!!!!");//never executing this block even if condition is true
                          removeIndex = index;
                     }
                })
                console.log(removeIndex); //its always 0
                user.posts.splice(removeIndex, 1);
                console.log(user.posts); //so its removing first post everytime

                User.findByIdAndUpdate(
                     {_id: userId},
                     {$set: {posts: user.posts}},
                     {new: true},
                     (err, nuser) => {
                          if(err)
                               return res.status(400).json({error:"Failed to remove post from user's database!"});
                          nuser.salt = undefined;
                          nuser.encry_password = undefined;
                          nuser.createdAt = undefined;
                          nuser.updatedAt = undefined;
                          res.json(nuser);
                })
           }
      });

 }

在此代码中,我正在获取用户 post 并希望从数组中删除特定 post 的 ID。我使用 map 函数遍历所有 postId 并通过 if 语句每次检查它是否是指定的一个。即使条件为真,块也不执行(它没有设置 removeIndex = index)

我将 postId 转换为字符串并且成功了。它应该是默认字符串,但无论如何,它起作用了。