Ottoman find() between models

Hi all,

I’m not sure if I’m missing something obvious here but can models perform finds on other models?

For example model A has a createAndSave method and wants to check that a referenced object doesn’t already exist in Couchbase before creating new one. I require the other model which is a child of Model A then call its find method. The callback method of that find doesn’t seem to get triggered.

Is this a bug. Please let me know if you need any code examples.

Cheers,

Fabrice

Hey @Fabrice,

Could you elaborate, perhaps with a small code snippet what you are trying to do?

Cheers, Brett

Hi @Brett19,

I have two files both representing a model each. One has reference to the other. lets say

parent.js

// assumes that couchbase and ottoman are setup and ready for action

const parent = ottoman.model("Parent", {
        child: [{ref: "Child"}],
        baz: "string"
})

parent.someFunction = function(param, callback) {
        child.find({foo: param}, function(err,result) {
            if (err) {
                console.log(err)
                callback(err, null)
            } else if (result && result.length > 0) {
                console.log('found')
                callback(null, 'found')
            } else {
                console.log('not found')
                callback(null, 'not found')
            }
        })
}

module.exports = parent

child.js

// assumes that couchbase and ottoman are setup and ready for action

const child= ottoman.model("Child", {
        foo: "string",
        bar: "string"
})
module.exports = child

Then in a controller, call the

parent.someFunction('blah', function(err, result){
    console.log(err,result)
})

The console.logs from someFunction never get called. If I take the child.find out of the someFunction and place that in the controller then all works as expected. Which is why I think there is a problem calling methods between models.

Cheers,

Fabrice

Hey @f.reynolds,

Might be a silly question, but have you imported child.js into your parent.js file? Also, can you try enabling debug mode to get more log information?

I know this might be less desirable, but any reason you can’t just process the logic to both models within your controller or some wrapping class?

Best,