2016-08-26 18 views
0

這是我的架構:新模型():假不可

var userScheme = mongoose.Schema({ 
aField:String, 
info: { 
    type:{ 
      local: { 
       email:String 
       } 
      }, 
    select:false 
     } 

}); 

當我嘗試創建一個新的用戶能正常工作:

var newUser = new User() 
newUser.aField="Something" 
newUser.save() 

但當我嘗試訪問具有select:false的字段時,我無法訪問數據。所以這不起作用:

var newUser = new User() 
newUser.aField="something" 
newUser.info.local.email="[email protected]" 
newUser.save() 

我得到的錯誤是:

TypeError: Cannot read property 'local' of undefined 

我的猜測是,原因是其被設置爲選擇新的模式是沒有的信息字段返回:假的。

如何讓新的Model()返回所有字段,包括那些設置爲'select:false'的字段?

謝謝!

回答

0

變成了select:false與它無關。

罪魁禍首是事實上,信息沒有默認值,並且根本沒有包含在模型中。

的解決方案是創建一個新的模式只是信息,並把它列入這樣的用戶模式:

var userScheme = mongoose.Schema({ 
    aField:String, 
    info: { 
     type:infoSchema, 
     default:infoSchema, //Without this, the new document will still not include an 'info' document, 
     select:false 
    } 

}); 

希望這有助於任何人。這只是我一生中的3個小時。