无法读取未定义的push属性?即使我将评论推送到评论数组,为什么仍会收到此错误?

Ali Azlan |

我将注释发布到/ arms /:id / comments,如下所示,但总是收到错误,无法读取push undefined的属性

router.post("/", middleware.isLoggedIn, function(req, res) {
  arms.findById(req.params.id, function(err, armsFound) {
    if (err) {
      console.log(err);
      res.redirect("back");
    } else {
      comments.create(req.body.comment, function(err, comment) {
        if (err) {
          console.log(err);
          res.redirect("back");
        } else {
          comment.author.id = req.user._id;
          comment.author.username = req.user.username;
          comment.save();
          arms.comments.push(comment);
          arms.save();
          req.flash("success", "Successfully added comment");
          res.redirect("/arms/" + arms._id);
        }
      });
    }
  });
});

arm schema在arm Schema注释中是一个数组,在Internet上的每个位置(包括stackoverflow),我都发现,如果我将某些内容推入数组,则可以,否则,它将返回错误。我还阅读了一些关于stackoverflow的内容,建议我使用实际的词(需要注释模式使用该词)来推送,在我的情况下为“ comments”,所以我写了“ comments”,而不是下面的代码中写有“ comment”的代码。但这也没有用

const mongoose = require("mongoose");

const armSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  price: String,
  image: String,
  description: String,
  owner: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    },
    username: String,
  },
  comments: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "Comment"
  }]
})

module.exports = mongoose.model("arms", armSchema);

注释架构

const mongoose = require("mongoose");

const commentSchema = new mongoose.Schema({
  text: String,
  author: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
    },
    username: String
  }
})

module.exports = mongoose.model("Comment", commentSchema);

崇乐

看起来在您的代码中arms是模型,而不是文档,因此arms.comments未定义。将其更改为armsFound将在您的回调中findById()解决该问题。像这样:

armsFound.comments.push(comment);
armsFound.save();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更新后的Angular 2我收到此错误:无法读取未定义的属性“区域”

当将redux-saga与react-native-web一起使用时,我们会收到错误消息“无法读取未定义的属性'mark'”

为什么我收到TypeError:无法读取未定义的属性“ now”

操作SDK会收到此错误TypeError:无法读取未定义的属性“输出”

为什么我会收到TypeError:即使我的Web元素引用正确,也无法读取未定义错误的属性“ getText”?

我收到此错误“ TypeError:无法读取未定义的属性'pagination'”

我在我的React项目上收到此错误“ TypeError:无法读取未定义的属性'map'”

我为什么会收到此错误:Uncaught TypeError:无法读取未定义的属性“ john”

为什么会收到“构造函数未定义”错误?

我收到错误消息:“无法读取未定义的属性'push'”

当我尝试在Swift中对动作进行排序时,为什么仍会收到此错误?

为什么我会收到该错误调用未定义方法Facebook \ Facebook :: api()?

为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”?

为什么我会收到此错误“NameError:name 'self' 未定义。”

为什么我收到错误“未捕获的类型错误:无法读取 HTMLLIElement 处未定义的属性‘样式’。”?

收到此错误:无法读取未定义的属性“0”

为什么即使我有空列表的回退,也会返回错误“无法读取未定义 [] 的属性“汽车”?

为什么我会收到未捕获的类型错误:如果声明了 var,则无法读取未定义的属性“长度”?

我收到此错误请帮助无法读取未定义的属性“地图”

我收到此错误 => TypeError:无法读取未定义的属性“图像”

收到此错误:无法读取未定义的属性“长度”

为什么我会收到此“主页未定义无未定义”错误

我收到类型错误:无法读取未定义的属性“推送”。我该怎么办?

为什么我收到错误:类型错误:无法读取未定义的“拆分”的属性

当我尝试使用 JavaScript 客户端连接到 Elasticsearch 时,为什么会收到错误“无法读取未定义的属性 'MAX_LENGTH'”?

为什么我会收到 TypeError:无法读取未定义的 react-router-dom 的属性“push”

为什么我收到错误 - TypeError:无法读取未定义的属性“名称”

为什么我会收到“类型错误:无法读取未定义的属性‘发送’”?

当我在我的反应应用程序中使用地图方法时,我收到此错误“无法读取未定义的属性(读取‘地图’)”