Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 信息不是';t通过Mongoose传递给数组,can';我不知道为什么_Javascript_Mongodb_Express_Mongoose - Fatal编程技术网

Javascript 信息不是';t通过Mongoose传递给数组,can';我不知道为什么

Javascript 信息不是';t通过Mongoose传递给数组,can';我不知道为什么,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,抱歉,如果之前有人回答过,我已经检查过其他答案,无法从这些答案中得出答案 我有一组信息,我希望将其放入名为“teamDetails”的数组中。以下是server.js中的相关/发布项: app.post('/create', (req, res) => { console.log('Post command received'); console.log(req.body); console.log(req.body.data.teamDetails[0]);

抱歉,如果之前有人回答过,我已经检查过其他答案,无法从这些答案中得出答案

我有一组信息,我希望将其放入名为“teamDetails”的数组中。以下是server.js中的相关/发布项:

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};
最后,我试图通过Postman注入的示例数据:

{
    "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推进去


感谢任何人提供的任何帮助。

看起来您在使用
newTeam.save()保存后添加了teamObjDetails。然后(…)

我对猫鼬不太熟悉,但我不知道如果不在保存前添加团队详细信息,怎么会出现

如果它改变了什么,请告诉我


A.G

使用那里的格式,我认为“新团队”在那之前并不存在,因此没有什么可推动的,因此我立即将其放在后面。我确实尝试过类似于你建议的东西,我把它作为一个对象,被推到另一个数组中,然后希望能被“newTeam.save”保存,但它从来没有这样做。谢谢您的回复。:)经过一些搜索后,您的模式可能会出现问题,而不是将teamDetails精确化为具有teamName、teamNameShort等的对象数组。。。你能添加一个TeamDetails架构并将TeamDetails指定为一个TeamDetails数组吗?所以,事实证明你是对的!我把事情搞砸了,还以为后来才给新团队打电话,但我错了!移动它,现在我得到一个新的错误,这是进步!错误现在是:“message:“Cast to ObjectID for value\”失败[{teamName:'Test teamName',\n teamNameShort:'TTN',\n teamfounded:'1986',\n teamHome:'London'}]\“at path\“\u id\”,我想这个错误是由您的模式引起的,我在官方文档中看不到这样的定义()因此,为您的团队详细信息创建子文档类型可能是一个不错的猜测:)我现在就开始,感谢您为我@antoine gautrain澄清这一点!