Javascript MongoDB阵列在POST后保持empy状态

Javascript MongoDB阵列在POST后保持empy状态,javascript,arrays,node.js,mongodb,mongoose,Javascript,Arrays,Node.js,Mongodb,Mongoose,如果预成型一个post请求(每个请求),则“游戏”数组保持为空。我错过什么了吗 更新:数组后出现新错误。推送({}) “errorValidationError:games.0.gameID:PathgameID是必需的,games.0.isGameActivated:PathisGameActivated是必需的,games.0.IsStartDateEastern:PathstartDateEastern是必需的。”。,games.0.hTeam:PathstartTimeISO是必需的,g

如果预成型一个post请求(每个请求),则“游戏”数组保持为空。我错过什么了吗

更新:数组后出现新错误。推送({}) “errorValidationError:games.0.gameID:Path
gameID
是必需的,games.0.isGameActivated:Path
isGameActivated
是必需的,games.0.IsStartDateEastern:Path
startDateEastern
是必需的。”。,games.0.hTeam:Path
startTimeISO
是必需的,games.0.hTeam:Path
hTeam
是必需的,games.0.hTeamScore:Path
hTeamScore
是必需的,games.0.hTeam:Path
hTeamTricode
是必需的。,games.0.vTeamScore:Path
vTeamScore
是必需的,games.0.vTeamTricode:Path
vTeamTricode
是必需的。”


您忘记在newGame对象上调用
save
newgame({…})
将只创建您创建的模型的对象。您需要调用对象上的
save
方法将其保存回数据库

newGame.save()

可以找到更多信息

更新后编辑 我注意到您在构建对象时也提供了_id字段。如果您打算对现有文档进行更新,那么我强烈建议您使用mongoose提供的
FindUpdate
样式中的一种。有几种方法可以使用,如
FindByAndUpdate
fin执行重复日期
或简单地将
upsert
的值设置为true的
更新
(如果与条件匹配的文档尚未存在,则将在upsert=true时创建该文档)


让我知道这是怎么回事。

您使用属性
games
定义模型,该属性中包含所有信息

创建模型时,未使用
games
属性

试试这个:

const newGame=新游戏({u id});
newGame.games.push({//your data here});
newGame.save();
明白了! 我太蠢了…它需要先循环请求,然后将其推送到数组中!下面是代码:

// ADD GAMES
router.route('/add').post((req, res) => {
    const _id = req.body._id;
    let games = [];
    req.body.games.forEach(game => {

        const gameID = game.gameID;
        const isStartTimeTBD = game.isStartTimeTBD;
        const isGameActivated = game.isGameActivated;
        const startTimeEastern = game.startDateEastern;
        const startDateEastern = game.startDateEastern;
        const startTimeISO = game.startTimeISO;
        const vTeam = game.vTeam;
        const vTeamTricode = game.vTeamTricode;
        const vTeamScore = game.vTeamScore;
        const hTeam = game.hTeam;
        const hTeamTricode = game.hTeamTricode;
        const hTeamScore = game.hTeamScore;

        games.push({
            gameID,
            isGameActivated,
            isStartTimeTBD,
            startDateEastern,
            startTimeEastern,
            startTimeISO,
            hTeam,
            hTeamScore,
            hTeamTricode,
            vTeam,
            vTeamScore,
            vTeamTricode,
        });
    });

    const newGame = new Game ({
        _id,
        games: games
    })

    newGame.save()
    .then(() => res.json('Game added cuz'))
    .catch(err => res.status(400).json('error' + err));

});


哦,糟糕,sceenshot没有显示它,但我调用了save()方法。这只是在MongoDB中添加新游戏。要更新这些集合,我将使用您提到的方法。谢谢!看看更新的答案。感谢您的回复,但现在我不断收到ValitaDioneErrors,尽管我提交的JSON是正确的。