2013-07-02 76 views
0

需要帮助解决这个问题,我想用填充方法来取代Id的一个reerenced文档,但是当我输出返回的结果时,文档不会返回,而是Id值仍然保持。这里是我的架构和细节代码clearify,感谢填充方法不返回文档 - Mongoose

var CommentSchema=new mongoose.Schema({ 
    comment:{type:String}, 
    accountId:{type:mongoose.Schema.ObjectId, ref:'Account'} 
    }); 


var StatusSchema=new mongoose.Schema({ 
comments:[CommentSchema], 
accountId:{type:mongoose.Schema.ObjectId, ref:'Account'},//should be replaced with docement 
      status:{type:String} 
     }); 

账户SCHEMA

var AccountSchema=new mongoose.Schema({ 
    email:{type:String,unique:true}, 
    password:{type:String}, 
    name:{ 
     first:{type:String}, 
     last:{type:String}, 
     full:{type:String} 
    }, 
    contacts:[Contact], 
    status:[Status] 
    }); 

var Account=mongoose.model('Account',AccountSchema); 
    var Comment=mongoose.model('Comment',CommentSchema); 
    var Status=mongoose.model('Status', StatusSchema); 

//这是我如何保存新的评论

app.post('/accounts/:id/status', function(req, res) { 
    var accountId = req.params.id; 

    models.Account.findById(accountId, function(account) { 
     account.save(function(err){ 
      if (err) throw err; 
     var status=new models.Account.Status(); 
     status.accountId=accountId; 
     status.status=req.param('status', ''); 
     status.save(function(err){ 
     account.status.push(status); 
     account.save(function(err){ 
      if(err)console.log(err) 
      else 
       console.log('successful! status save...') 
     }); 
     }); 
     }); 
    }); 
    res.send(200); 
    }); 

// QUERY -I想到这查询以用所引用的文档替换accountId,但是当查询运行时,accountId仍然保存id而不是引用的文档

var getActivity= function(accountId,callback){ 
    Account.findOne({_id:accountId}).populate('Status.accountId').exec(function(err, items) { 
        console.log('result is:'+items); 
      }); 
     } 
+0

我认为这应该只是'填入(“状态”)' – Benoir

+0

感谢的是,我仍然有thesame结果 –

+0

问题仍然存在 –

回答

0

在定义之前,您正在使用StatusAccountSchema。但是,无论如何,您实际上应该使用StatusSchema(模式)而不是Status(模型)。

试试这个:

var AccountSchema=new mongoose.Schema({ 
    email:{type:String,unique:true}, 
    password:{type:String}, 
    name:{ 
     first:{type:String}, 
     last:{type:String}, 
     full:{type:String} 
    }, 
    contacts:[Contact], 
    status:[StatusSchema] 
}); 
+0

谢谢,但我也用'帐户'在它被定义之前,在StatusChema和CommentSchema中,是否也会影响它?似乎他们都依赖对方 –

+0

谢谢,我仍然得到像以前一样的问题 –

+0

问题仍然存在 –