2014-07-06 17 views
0

我試圖訪問一個商店地址的屬性,但我一直在'未定義'。原來,地址只是一個ID,即使我在地址的模式中聲明瞭'ref'。我在這裏做錯了什麼?當我試圖引用另一個模式的對象時,爲什麼不讓mongoose ref爲我工作?

從initStores輸出

{ name: 'Wegmans', 
    address: 53b998d9fcf740e07a4d03f7, 
    _id: 53b998d9fcf740e07a4d03f8, 
    items: [] } 
53b998d9fcf740e07a4d03f7 
undefined 

路由器/ index.js

router.post('/initStores', function(req, res) { 
    var address = new Address({ 
     street:  '650 Hylan Dr', 
     city:  'Rochester', 
     state:  'NY', 
     zipcode: '14623', 
     telephone: '123-456-7890' 
    }); 
    // address.save(); 

    var store = new Store({ 
     name: 'Wegmans', 
     address: address 
    }); 

    console.log(store); 
    console.log(store.address); 
    console.log(store.address.street); 
    // store.save(); 
} 

模型/ address.js

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

var AddressSchema = new Schema({ 
    type:  { type: String, default: 'Store' }, // Store, apartment, house, headquarter 
    street: { type: String, require: true }, 
    city:  { type: String, require: true }, 
    state:  { type: String, require: true }, 
    zipcode: { type: String, require: true }, 
    country: { type: String, default: 'United States' }, 
    telephone: { type: String, default: '555-555-5555' } 
}); 

mongoose.model('Address', AddressSchema); 

模型/ store.js

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

var StoreSchema = new Schema({ 
    name:  { type: String, require: true }, 
    address: { type: ObjectId, ref: 'Address' }, 
    items:  [ { type: ObjectId, ref: 'Item' } ] 
}); 

mongoose.model('Store', StoreSchema); 

回答

2

對於引用模式,此代碼完全按設計工作。 「地址」模式中的字段完全不是「存儲」模式的一部分。這裏引用的設計只是存儲相關文檔的_id值,其中該文檔的其餘部分將駐留在其自己的集合中。

所以,實際上你實際上是在分別創建這些文件,並且只有在執行.populate()操作之後,纔會將不同文檔實際上「顯示」爲「連接」,就如同文件實際上以本地方式出現一樣「嵌入」。

Store.findById(store._id).populate("address").exec(err,newstore) { 

    console.log(newstore); 
}); 

還是因爲你真的有「商店」對象已經並確保「拯救」已經完成:

async.series(
    [ 
    function(callback) { 
     address.save(function(err,address) { 
     callback(); 
     }); 
    }, 
    function(callback) { 
     store.save(function(err,store) { 
     Store.populate(store,{ path: "address"},function(err,store) { 
      console.log(store); 
      callback(); 
     }); 
     }); 
    } 
    ] 
); 

在此可以與「應該」的出現作爲一個整體對象的唯一途徑沒有它需要保存的數據.populate(),或者是通過嵌入的地址,它是「商店」的一部分,而不是保存到一個不同的模型和收集:

var StoreSchema = new Schema({ 
    name:  { type: String, require: true }, 
    address: { 
     "type": { type: String, default: 'Store' }, // Store, apartment, house, headquarter 
     street: { type: String, require: true }, 
     city:  { type: String, require: true }, 
     state:  { type: String, require: true }, 
     zipcode: { type: String, require: true }, 
     country: { type: String, default: 'United States' }, 
     telephone: { type: String, default: '555-555-5555' } 
    }, 
    items:  [ { type: ObjectId, ref: 'Item' } ] 
}); 

或者,如果你的目的是在創作只是單純的日誌記錄,那麼你可以轉換和操作與.toObject()

var raw = store.toObject(); 
raw.address = address.toObject(); 

console.log(raw); 

但以其他方式與參考模型,其意圖是,數據是不一樣的對象的一部分,直到.populate()被調用。

+0

最初我想將地址存儲在不同的集合中,但現在我沒有看到您指出它只能使用Store的地址屬性的對象,並且該地址不會被無論如何,除了那家特定的商店以外的其他東西並感謝您告訴我有關填充方法!我不知道你是如何訪問其他模式屬性的。 – Vongdarakia

相關問題