奥斯曼帝国不解决参考的问题

时间:2019-01-31 07:28:36

标签: node.js couchbase couchbase-ottoman

我的脚凳1.0.5设置中有两个模型。一个拥有联系信息,其中包括一组电子邮件文档,然后是电子邮件文档。我可以在文档中插入新的联系人以及电子邮件,并在联系人文档中插入新电子邮件的相应链接。

这是我的模特

const ottoman = require("ottoman")
ottoman.bucket = require("../app").bucket
var ContactModel = ottoman.model("Contact",{
    timestamp: {
        type: "Date",
        default: function() {return new Date()}
    },
    first_name : "string",
    last_name : "string",
    emails: [
        {
            ref:"Email"
        }
    ]}  )
var EmailModel = ottoman.model("Email",{
timestamp: {
    type: "Date",
    default: function() {return new Date()}
},
type : "string",
address : "string",
name: "string"
} )
module.exports = {
ContactModel : ContactModel,
EmailModel : EmailModel
}

现在我可以使用此功能来获取联系人及其所有电子邮件

app.get("/contacts/:id",  function(req, res){
model.ContactModel.getById(req.params.id,{load: ["emails"]}, function(error, contact){
if(error) {
res.status(400).json({ Success: false , Error: error, Message: ""})
      }
 res.status(200).json({ Success: true , Error: "", Message: "", Data : contact})
})
})

这是谁给我的

{
"Success": true,
"Error": "",
"Message": "",
"Data": {
"timestamp": "2019-01-30T23:59:59.188Z",
"emails": [
    {
        "$ref": "Email",
        "$id": "3ec07ba0-aaec-4fd4-a207-c4272cef8d66"
    }
],
"_id": "0112f774-4b5d-4b73-b784-60fa9fa2f9ff",
"first_name": "Test",
"last_name": "User"
}
}

如果我将联系人记录到控制台,我会得到这个

OttomanModel(`Contact`, loaded, key:Contact|0112f774-4b5d-4b73-b784-60fa9fa2f9ff, {
timestamp: 2019-01-30T23:59:59.188Z,
emails: [ OttomanModel(`Email`, loaded, key:Email|3ec07ba0-aaec-4fd4-a207-c4272cef8d66, {
 timestamp: 2019-01-31T00:36:01.264Z,
 _id: '3ec07ba0-aaec-4fd4-a207-c4272cef8d66',
 type: 'work',
 address: 'test@outlook.com',
 name: 'Test Outlook',
 }),
 OttomanModel(`Email`, loaded, key:Email|93848b71-7696-4ef5-979d-05c19be9d593, {
 timestamp: 2019-01-31T04:12:40.603Z,
 _id: '93848b71-7696-4ef5-979d-05c19be9d593',
 type: 'work',
 address: 'newTest@outlook.com',
 name: 'Test2 Outlook',
 }) ],
 _id: '0112f774-4b5d-4b73-b784-60fa9fa2f9ff',
 first_name: 'Test',
 last_name: 'User',
 })

这表明电子邮件已解决,但为什么它不显示在返回的json中。另一方面,如果我返回contact.emails,我会收到已解决的电子邮件。因此,我希望有人能阐明我在这里缺少的东西

1 个答案:

答案 0 :(得分:0)

我在couchbase论坛上问了类似的问题,并且我也找到了解决方案: (我的搜索结果是一个数组,而不是您所遇到的对象,这有点不同)

forum.couchbase.com

 app.get("/assets", (req, res) => {
    AssetModel.find({}, { load: ["assetModelId", "assetGroupId", "assetTypeId"] }, (err, results) => {
        if (err) return res.status(400).send("no asset found");
        const assets = [];
        results.map(asset => {
            assets.push({...asset});
        });
        res.status(200).send(assets)
    });
});