提问者:小点点

插入:无法读取未定义的属性“Push”


这是我的第一个项目,也是我的第一个岗位。我正在创建一个在朋友之间使用的博彩网站,但我遇到了这个错误:无法读取未定义的属性“Push”

我想让它再次发挥作用是件小事吧?

有什么建议吗?

router.post("/bets", function(req, res){
    var betHome = req.body.betHome;
    var betAway = req.body.betAway;
    var matchid= "5e84bd729511b98c34101a9e"
    var author = {
        id: req.user._id,
        username: req.user.username
    }
    var newBet = {bet:[betHome,betAway],author:author, matchid:matchid}
    Bet.create(newBet, function (err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            Match.betsList.push(newBet)
           res.redirect("/bets");
           console.log(newBet)

        }
        })
})

module.exports = router;

数据库模式


共1个答案

匿名用户

不能直接推入模型,先创建匹配模型的对象,然后再推入。

   router.post("/bets", function(req, res){
    var betHome = req.body.betHome;
    var betAway = req.body.betAway;
    var matchid= "5e84bd729511b98c34101a9e"
    var author = {
        id: req.user._id,
        username: req.user.username
    }
    var newBet = {bet:[betHome,betAway],author:author, matchid:matchid}
    Bet.create(newBet, function (err, newlyCreated){
        if(err){
            console.log(err);
        } else {
         var match = new Match({});  // new Schema
            match.betsList.push(newlyCreated._id)
           res.redirect("/bets");
           console.log(newBet)

        }
        })
})

module.exports = router;