信息不是通过Mongoose传递给数组的,无法弄清楚为什么

时间:2019-03-15 14:45:29

标签: javascript mongodb express mongoose

很抱歉,如果以前已经回答过,我已经检查了其他答案,但无法从中得出答案。

我有一组信息,希望将它们放入名为“ teamDetails”的数组中。这是server.js中的相关/ post项:

app.post('/create', (req, res) => {
    console.log('Post command received');
    console.log(req.body);
    console.log(req.body.data.teamDetails[0]);
    //We need to push the variable below, 'teamDetails', as an object into an array of the same name
    var teamDetailsObj = {
    // Modified for Postman
    "teamName": req.body.data.teamDetails[0].teamName,
    "teamNameShort": req.body.data.teamDetails[0].teamNameShort,
    "teamfounded": req.body.data.teamDetails[0].teamFounded,
    "teamHome": req.body.data.teamDetails[0].teamHome
    };
    console.log(teamDetails);
    var newTeam = new Team({
        "data.added": new Date(),
        "data.entry": req.body.data.entry
    });

    newTeam.save().then((doc) => {
        console.log("This is newTeam.data: " + newTeam.data);
        console.log("This is teamDetailsObj: " + teamDetailsObj);
        newTeam.data.teamDetails.push(teamDetailsObj);
        var teamId = doc.id;
        res.render('success.hbs', {teamId});
        console.log("Team Added - " + teamId);
    }, (e) => {
        res.status(400).send(e);
    });
});

这是我的team.js模型:

var mongoose = require('mongoose');
var ObjectID = mongoose.Schema.Types.ObjectId;
var Mixed = mongoose.Schema.Types.Mixed;
var Schema = mongoose.Schema;

var Team = mongoose.model('Team', {
  data: {
    entry: {
      type: String,
      default: "USER.INPUT"
    },
    added: {
      type: Date,
      default: Date.Now
    },
    teamDetails: [
      {
        teamName: {
          type: String,
          trim: true,
          required: true,
          default: "First Team"
        },
        teamNameShort: {
          type: String,
          trim: true,
          uppercase: true,
          maxlength: 3,
          required: true
        },
        teamFounded: {
          type: Number,
          maxlength: 4
        },
        teamHomeCity: {
          type: String
        }
      }
    ]
  }
});

module.exports = {Team};

最后,我正尝试通过邮递员注入的示例数据:

{
    "data": {
        "entry": "Postman.Entry",
        "teamDetails": [
            {
                "teamName": "Test Teamname",
                "teamNameShort": "TTN",
                "teamFounded": "1986",
                "teamHome": "London",
                "players": [
                    {
                    "player1Name": "Test Player 1",
                    "player1Position": "Forward",
                    "player1Nationality": "GBR"
                    },
                    {
                    "player2Name": "Test Player 2",
                    "player2Position": "Defender",
                    "player2Nationality": "UKR"
                    },
                    {
                    "player3Name": "Test Player 3",
                    "player3Position": "Goaltender",
                    "player3Nationality": "IRL",
                    "captain": true
                    }
                ],
                "coachingStaff": {
                    "headCoach": "Derp McHerpson",
                    "teamManager": "Plarp McFlarplarp"
                    }
                }
            ]
        }
}

(忽略玩家部分,那是另一锅鱼)

由于使用了上面的代码,teamDetails的结果条目只是一个空数组。我只是无法获取将teamDetailsObj推送到其中的代码。

任何人都能提供的帮助将受到赞赏。

1 个答案:

答案 0 :(得分:1)

使用newTeam.save().then( ... )保存后,您似乎添加了teamObjDetails

我对Mongoose不太熟悉,但是我看不到如果在保存之前不添加团队详细信息,怎么会出现。

让我知道它是否会有所改变!

A。 G