Problem with ottoman not resolving the references

197 Views Asked by At

I have two models in my ottoman 1.0.5 setup. One holds contact info which includes an emails array of docs and then the email doc. I can insert new contacts fine as well as emails in docs and the corresponding link in the contact doc for the new email.

Here is my model

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
}

Now to get an contact and all its emails i use this function

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})
})
})

Which returns me this

{
"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"
}
}

if i go and log the contact to my console i get this

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: '[email protected]',
 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: '[email protected]',
 name: 'Test2 Outlook',
 }) ],
 _id: '0112f774-4b5d-4b73-b784-60fa9fa2f9ff',
 first_name: 'Test',
 last_name: 'User',
 })

This shows that emails was resolved but why does it not show up in the returned json. On the other hand if i return contact.emails i get the resolved emails just fine. So i hope someone can shed some light on what i am missing here

1

There are 1 best solutions below

0
On

I asked a similar question on the couchbase forum, and I also found out the solution: (a slight difference that the result of my search is an array not an object like in your case)

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