2016-01-31 55 views
0

我有一個方法來更新商業信息。通過在請求正文中發送工作日,可能會將工作日添加到商務中。 下面的代碼工作正常,除了工作日不是在mongoDB中創建的。它們僅保存在Commerce文檔中(作爲數組odid),但名爲「workday」的集合未在mongoDB中創建。爲什麼沒有創建?Mongoose子文檔沒有在mongoDB中創建,但添加到父數組

  if(req.body.workdays){ 
       var workdays = req.body.workdays; 
       var lunch = req.body.lunch.split("_"); 

       commerce.workdays=[]; 
       for(var i =0, size=workdays.length; i<size; i++){ 
        var item=new Workday(); 
        item.dayOfWeek = workdays[i]; 
        item.owner=commerce._id; 
        var range = new Range(); 
        range.initial = lunch[0]; 
        range.end = lunch[1]; 
        range.workday = item; 
        item.ranges.push(range); 
        commerce.workdays.push(item); 
       } 
       } 

       commerce.save(function(err) { 
       if(!err) { 
        log.debug('Updated'); 
        res.status(200).send(commerce); 
       } else { 
        errorHandler.processError(err, res, log); 
       } 
       }); 

這裏的模型:

var CommerceSchema = new Schema({ 
    // Common fields. 
    createdAt : {type : Date, default : Date.now}, 
    location: [Number, Number], 
    photos: [{type : Schema.Types.ObjectId, ref : 'Photo'}], 
    name: { type: String}, 
    address: { type: String}, 
    email: { type: String, default: "-"}, 
    workdays: [{type : Schema.Types.ObjectId, ref : 'Workday'}], 
    description: { type: String, default: "-"}, 
    phone: { type: Number}, 
    user: {type : String, ref : 'User'}, 
    type: [{ type: Number, default: 0}] 
}); 

var WorkdaySchema = new Schema({ 
    dayOfWeek: { type: Number}, 
    owner: {type : String}, 
    ranges: [{type : Schema.Types.ObjectId, ref : 'Range'}], 
    createdAt : {type : Date, default : Date.now} 
}); 

var RangeSchema = new Schema({ 
    initial: { type: Number}, 
    end: { type: Number}, 
    workday: {type : String, ref : 'Workday'} 
}); 

回答

2

「工作日」 期待蒙戈的ObjectID。您必須首先保存個人工作日,然後您可以將他們的ID(_id)添加到工作日數組中。

相關問題