Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB从body中查找、更新和验证空字段_Javascript_Node.js_Mongodb_Mongodb Query - Fatal编程技术网

Javascript MongoDB从body中查找、更新和验证空字段

Javascript MongoDB从body中查找、更新和验证空字段,javascript,node.js,mongodb,mongodb-query,Javascript,Node.js,Mongodb,Mongodb Query,在更新字段之前,我需要检查以下值是否为空。 因此,我按照以下方式构建,但不检索msg错误,也不允许我向配置文件用户升级 router.post("/profileEdit/:id", ensureAuthenticated, (req, res) => { var id = mongoose.Types.ObjectId(req.params.id); let errors = []; if (!req.body.firstName) { errors.push({ t

在更新字段之前,我需要检查以下值是否为空。 因此,我按照以下方式构建,但不检索msg错误,也不允许我向配置文件用户升级

router.post("/profileEdit/:id", ensureAuthenticated, (req, res) => {
  var id = mongoose.Types.ObjectId(req.params.id);
  let errors = [];
  if (!req.body.firstName) {
    errors.push({ text: "Please add your first name" });
  }
  if (!req.body.lastName) {
    errors.push({ text: "Please add your last name" });
  }
  if (!req.body.username) {
    errors.push({ text: "Please add your username" });
  }
  if (!req.body.email) {
    errors.push({ text: "Please add you email" });
  }

  if (errors.length > 0) {
    res.render("users/profileEdit", {
      errors: errors,
      firstName: req.body.firstName,
      lastName: req.body.lastName,
      username: req.body.username,
      email: req.body.email
    });
  } else {
    User.updateOne(
      { _id: id },
      {
        $set: {
          firstName: req.body.firstName,
          lastName: req.body.lastName,
          username: req.body.username,
          email: req.body.email
        }
      }
    ).then(user => {
      req.flash("success_msg", "Profile Updated");
      res.redirect("/users/profile/" + id);
    });
  }
});

如何验证每个字段的req.body是否为空?在验证之后,我需要更新mongodb内部的信息。

解决方案如下:

在执行其他操作之前,我只需要找到_id:id,这样,由于上面的字段是空的,应用程序将返回相同的页面并填充字段

router.post("/profileEdit/:id", ensureAuthenticated, (req, res) => {
  var id = mongoose.Types.ObjectId(req.params.id);
  let errors = [];

  if (!req.body.firstName) {
    errors.push({ text: "Please add your first name" });
  }
  if (!req.body.lastName) {
    errors.push({ text: "Please add your last name" });
  }
  if (!req.body.username) {
    errors.push({ text: "Please add your username" });
  }
  if (!req.body.email) {
    errors.push({ text: "Please add you email" });
  }

  if (errors.length > 0) {
    User.find(
      { _id: id },
      {
        firstName: 1,
        lastName: 1,
        username: 1,
        email: 1,
        password: 1,
        _id: { $elemMatch: { _id: id } },
        date: 1
      }
    ).then(user => {
      res.render("users/profileEdit", {
        user: user
      });
    });
  } else {
    User.updateOne(
      { _id: id },
      {
        $set: {
          firstName: req.body.firstName,
          lastName: req.body.lastName,
          username: req.body.username,
          email: req.body.email
        }
      }
    ).then(user => {
      req.flash("success_msg", "Profile Updated");
      res.redirect("/users/profile/" + id);
    });
  }
});

那么,什么不起作用呢?@Carlos Orelhas:虽然你可以用js来做,但在node.js的世界里,使用它是很常见的,看看它可能会对你有所帮助。@technophyle我上面展示的方式是行不通的。。无法验证字段验证代码在我看来很好。可能是您的
profileEdit
模板有问题。两者都可以正常工作,但我认为我需要首先找到id,然后进行验证。。由于这种方式,请在profileEdit中显示多行