我正在尝试提出一个Mongoose模式布局以匹配我需要的最终JSON布局

时间:2019-03-13 17:18:39

标签: javascript mongodb mongoose

我意识到这是购物车场景之前的一匹马,但是我发现我需要传递一些JSON格式,现在我正试图让Mongoose保存信息以进行匹配,但是我发现无法将信息添加到信息中的数组中。

代码:(team.js)

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

var player = new Schema({
      player:  {
        playerName: {
          type: String,
          trim: true,
          required: true
        },
        playerNumber: {
          type: Number,
          minlength: 1,
          maxlength: 2
          }
        },
        playerPosition: {
          type: String
      }
});

var Team = mongoose.model('Team', {
  data: {
    entry: {
      type: String,
      default: "StatCaster2019.USER.INPUT"
    },
    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
        },
        players: [ player ],
      }
    ]
    }
});

module.exports = {Team};

代码:(server.js)

app.post('/create', (req, res) => {
    console.log('Post command received');
    var players = [];
    var playerObj = {};
    for (let i = 1; i < 21; i++) {
        var playerObj = { playerName: req.body[`player${i}Name`], playerNumber: req.body[`player${i}Number`], playerPosition: req.body[`player${i}Position`] };
        if (req.body["player" + i + "Name"] === '') {
            console.log("Empty player name detected, disregarding");
        } else {
            players.push(playerObj);
        }
    }

    var newTeam = new Team({
        // POSTMAN SETUP BELOW
        // "team": req.body.team[0],
        // "teamName": req.body.team[0].teamName,
        // "shortTeamName": req.body.team[0].shortTeamName,
        // "teamRoster": req.body.team[0].teamRoster,
        // "teamCoach": req.body.team[0].teamCoach

        // WEB SETUP BELOW
        "team.teamRoster.teamCoach": req.body.coachName,
        "team.shortTeamName": req.body.teamShortName,
        "team.teamName": req.body.teamName,
        "team.teamRoster.players": players,
        "team.added": new Date(),
        "team.updated": new Date()
    });

    // console.log(req.params);

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

现在,在玩MongoDB Atlas时,我对如何显示它有了一个理想的布局,以便它可以与另一个程序进行交互。 (从本质上讲,该程序要求JSON是数组中的数据对象,我发现很难对其进行排列,但这是可行的...)

id:5c87f7961c9d440000ce3296
entry:"StatCaster2019.TEMPLATE.FOR.TWO.TEAMS.INFORMATION"
teamDetails: Array
    0:Object
        teamName: "Rival Teamname"
        teamNameShort: "DERP"
        teamFounded: "1924"
        teamHome: "Southhampton"
        players: Array
            0: Object
                playerName: "Derp Herpson"
                playerPosition: "Forward"
                playerNationality: "GBR"
                playerNumber: "47"
                playerAge: "24"

以上代码仅是示例,与team.js类别相比,它具有更多类别。由于我无法解决的原因,任何尝试在数组中添加的操作都无法通过。

一如既往,我们始终感谢您的帮助。

0 个答案:

没有答案