繁体   English   中英

推不适用于猫鼬/ mongodb数组

[英]push not working in mongoose/mongodb array

试图创建一个简单的系统,用户可以在其中查看书,但是我一直收到“ push”的错误,但未定义。 我尝试过多次向数据库发送新评论,但每次都失败。

我有一个猫鼬模型:

var WorkSchema = new mongoose.Schema({
    title: String,
    genre: String,
    workType: String,
    length: Number,
    ageRange: String,
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    },
    manuscriptText: String,
    workRating: [
        {
            reviewerName: String,
            critique: String,
            date: Date
        }
    ],
    ratingNumber: [Number],
    ratingSum: {
        type: Number,
        default: 0
    }
});

这是我的评论文章,其中包含所有注释掉的失败代码:

// post route for getting the review
router.post('/:id', function(req, res) {

    var critique = req.body.critique;
    var reviewerName = req.user.username;

    // find the right work associated with the critique
    Work.findById(req.params.id, function(err, foundWork) {
        if(err) {
            console.log(err);
        } else {

            // foundWork.workRating.reviewerName.push(reviewerName);
            // foundWork.workRating.critique.push(critique);
            // foundWork.workRating.date.push(Date());
            // foundWork.save();

            //     });
            // }
            // foundWork.update(
            //     {$push: {workRating: }
            //     }
            // );
            // {
            //     $push: {
            //         workRating: {
            //             reviewerName: reviewerName
            //             reviewerReview: critique
            //         }
            //         // ratingNumber: req.body.clickedValue,
            //         // $inc: {
            //         //     ratingSum req.body.clickedValue
            //         // }
            //     }
            // }
        }
    });
});

我将这两个值放入该数组到底在做什么呢?

您所做的事情有很多错误。 push是将元素添加到数组中的函数, reviewerNamecritiquedate都不是数组。 这就是为什么您看到.push is undefined

看起来像是您正在尝试通过在workRating数组中push一个对象来更新文档。 所以,这就是你的做法

Work.update({_id :req.params.id }, {$push : {workRating : {reviewerName : "A", 
critique : "XYZ", date : "your_date"}}})

因此,您在尝试中的错误地方有一些问题,并且还有更好的方法来处理此问题

只需在模型上直接使用.updateOne()而不是findById()

Work.updateOne(
  { "_id": req.params.id },
  { 
    "$push": {
      "workRating": { 
        "reviewerName": reviewerName,
        "critique": critique,
        "date": new Date()
      },
      "ratingNumber": req.body.clickedValue
    },
    "$inc": {
      "ratingSum": req.body.clickedValue
    }
  },
  function(err, response) {
   // handling
  }
)

当您实际上要“更新一个”文档时, .updateOne()在现代API中是“首选”。 update()方法做同样的事情,只更新“第一个匹配项”,但是相对于代码中更“描述性”的方法而言,它的用法被“弃用”了。

或者,如果您确实希望返回文档.findByIdAndUpdate()

Work.findByIdAndUpdate(req.params.id,
  { 
    "$push": {
      "workRating": {
        "reviewerName": reviewerName,
        "critique": critique,
        "date": new Date()
      },
      "ratingNumber": req.body.clickedValue
    },
    "$inc": {
      "ratingSum": req.body.clickedValue
    }
  },
  { "new": true },             // need to get the "modified" document
  function(err, foundWork) {
    // handling
  }
)

您基本上将修饰符放在错误的位置,而实际上不需要“获取”时,简单地“更新”会更有效。

或采用“获取/修改/保存”的“不太好”模式:

foundWork.workRating.push({
  "reviewerName": reviewerName,
  "critique": critique,
  "date": new Date()
});
foundWork.ratingNumber.push(req.body.clickedValue);
foundWork.ratingSum = foundWork.ratinSum + 1;

foundWork.save(function(err,modifiedWork) {
  // handling
});

再一次,您实际上只是试图在错误的位置进行.push()

请注意,您也可以添加架构:

"date": { "type": Date, "default": Date.now }

它会自动将该值应用于所有操作,因为猫鼬会根据架构设置修改更新操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM