2016-02-28 205 views
0

一組我有這個在我的貓鼬架構...一些組...如何填充在貓鼬

'use strict'; 

var mongoose = require('mongoose') 
, Schema = mongoose.Schema; 

var clientSchema = new mongoose.Schema({ 

    name        : { type: String }, 
    offerings     : [{ type: String }], 
    cscPersonnel    : { 
     salesExec     : { type: Schema.Types.ObjectId, ref: 'User' }, 
     accountGM     : { type: Schema.Types.ObjectId, ref: 'User' }, 
}, 
    }, 
    netPromoterScore  : { type: Number } 

}); 

module.exports = mongoose.model('clients', clientSchema); 

我試圖填充REFF DIS辦法......我也居住在REF(用戶爲{路徑: 'cscPersonnel'})

function getOneById(id){ 
    var deferred = Q.defer(); 
console.log("im in get by id" +id); 
    model 
     .findOne({ _id: id }) 
     .populate({path:'cscPersonnel'})//one way 
      /* 'cscPersonnel salesExec', //second way 
      'cscPersonnel accountGM', */ 
     .exec(function (err, item) { 
      if(err) { 
       console.log(err); 
       deferred.reject(err); 
      } 
      else 
       console.log(item); 
       deferred.resolve(item); 
     }); 

    return deferred.promise; 
} // gentOneById method ends 

但不幸的是結束了與此錯誤!!!!

CastError:投射到的ObjectId失敗值「的翻譯:」在路徑「_id」

{ 
    "message": "Cast to ObjectId failed for value \"[object Object]\" at path \"_id\"", 
    "name": "CastError", 
    "type": "ObjectId", 
    "value": { 
    "salesExec": "56cf5f09245f8a240b30693b", 
    "accountGM": "56cf5f09245f8a240b30693b" 
    }, 
    "path": "_id" 
} 

如何使解決這一問題.... 做幫助,在此先感謝

回答

1

請真正嘗試這一個

model 
    .findOne({ _id: id }) 
    .populate({path: 'cscPersonnel.salesExec'}) 
    .populate({path: 'cscPersonnel.accountGM'}) 
    .exec(function (err, item) { 
+0

三江源soooooooooooooo多...生活救星!!!!!!!!!!!!!! –

+0

@swatikiran,請問你的新問題的另一個問題。 – zangw