嵌入式阵列推送无法在mongoose中工作 - 推送未定义

时间:2014-06-24 07:47:12

标签: node.js mongodb mongoose database-schema

枝,

我有这种格式的架构。

var parentSchema = mongoose.Schema({
    username: { type: String, required: true, index: { unique: true } },
    password: { type: String, required: true },
    parent_id : Schema.Types.ObjectId,
    country_id : Schema.Types.ObjectId,
    child: [childSchema]
});

var childSchema = mongoose.Schema({
    child_name: String,
    standard_id: Schema.Types.ObjectId,
    child_id : Schema.Types.ObjectId,
    total_gold: { type: Number},
    total_diamonds:{ type: Number },
    total_hearts: { type: Number},
    subjects: [subjectSchema]
});

var subjectSchema = mongoose.Schema({
    subject_name :String,
    grade_name : String,
    games: [gameSchema]
});

var gameSchema = mongoose.Schema({
    id:Schema.Types.ObjectId,
    game_id: Schema.Types.ObjectId,
    gold: { type: Number },
    diamonds: { type: Number },
    hearts: { type: Number },
    plays: { type: Number },
    best_score: { type: Number },
    is_locked: String
});

我试图以这种方式将数据插入到模型中。

var parent = new Parent();
parent.username = req.body.username;
parent.password = req.body.password;

var games = new Games({gold:0,diamonds:0,hearts:0,plays:0,best_score:0,is_locked:false});


var subjects = new Subjects({subject_name:req.body.subjectname,grade_name:req.body.gradename});

var child = new Child({child_name:req.body.childname,total_gold:0,total_diamonds:0,total_hearts:0});

将数据插入到模型中后,iam以这种方式将模型添加到父文档中。

subjects.games.push(games);
child.subjects.push(subjects);
parent.child.push(child);
parent.save(function (err, parent) {
    if (err)
        throw err;
});

当我使用上面的代码时,我收到错误,说主题没有定义推送。

错误堆栈如下:

 subjects.games.push(games);
                           ^
 TypeError: Cannot call method 'push' of undefined
at Promise.module.exports.saltAndHash (d:\BWApis\BWApiProject\app\server\router.js:926:32)
at Promise.onResolve (d:\BWApis\BWApiProject\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (d:\BWApis\BWApiProject\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
at Promise.fulfill (d:\BWApis\BWApiProject\node_modules\mongoose\node_modules\mpromise\lib\promise.js:97:20)
at Object.cb (d:\BWApis\BWApiProject\node_modules\mongoose\lib\query.js:1145:30)
at Object.exports.tick (d:\BWApis\BWApiProject\node_modules\mongoose\node_modules\mquery\lib\utils.js:137:16)
at processImmediate [as _immediateCallback] (timers.js:330:15)

1 个答案:

答案 0 :(得分:0)

我认为你在这里做错了。

subjects.games.push(games);
child.subjects.push(subjects);
parent.child.push(child);
parent.save(function (err, parent) {
    if (err)
        throw err;
});

试试这个 - 虽然它会起作用根据你的问题,但这也是一个错误的方法

games.save(function(er, newGame) {
    if (err)
        throw err;
    subjects.games.push(newGame);
    subject.save(function(err, newSubject) {
        if (err)
            throw err;
        child.subjects.push(newSubject);
        child.save(function(err, newChild) {
            if (err)
                throw err;
            parent.child.push(newChild);
            parent.save(function (err, parent) {
                if (err)
                    throw err;
            });
        })        
    })
})

您的架构结构错误

您应该使用此架构结构

var parentSchema = mongoose.Schema({
    username: { type: String, required: true, index: { unique: true } },
    password: { type: String, required: true },
    parent_id : Schema.Types.ObjectId,
    country_id : Schema.Types.ObjectId,
    child: [{ type: Schema.Types.ObjectId, ref: 'Child'}] //Note: Child is model name not any name or Schema Name
});

var childSchema = mongoose.Schema({
    child_name: String,
    standard_id: Schema.Types.ObjectId,
    child_id : Schema.Types.ObjectId,
    total_gold: { type: Number},
    total_diamonds:{ type: Number },
    total_hearts: { type: Number},
    subjects: [{ type: Schema.Types.ObjectId, ref: 'Subjects'}]
});

var subjectSchema = mongoose.Schema({
    subject_name :String,
    grade_name : String,
    games: [{ type: Schema.Types.ObjectId, ref: 'Games'}]
});

var gameSchema = mongoose.Schema({
    id:Schema.Types.ObjectId,
    game_id: Schema.Types.ObjectId,
    gold: { type: Number },
    diamonds: { type: Number },
    hearts: { type: Number },
    plays: { type: Number },
    best_score: { type: Number },
    is_locked: String
});



games.save(function(er, newGame) {
    if (err)
        throw err;
    subjects.games.push(newGame._id);
    subject.save(function(err, newSubject) {
        if (err)
            throw err;
        child.subjects.push(newSubject._id);
        child.save(function(err, newChild) {
            if (err)
                throw err;
            parent.child.push(newChild._id);
            parent.save(function (err, parent) {
                if (err)
                    throw err;
            });
        })        
    })
})