【问题标题】:Problem with ottoman not resolving the references奥斯曼无法解决参考的问题
【发布时间】:2019-01-31 07:28:36
【问题描述】:

我的 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,我会收到已解决的电子邮件。所以我希望有人能阐明我在这里所缺少的东西

【问题讨论】:

    标签: node.js couchbase couchbase-ottoman


    【解决方案1】:

    我在 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)
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2014-06-21
      • 2014-02-09
      • 1970-01-01
      相关资源
      最近更新 更多