Get all collection records related to a model

92 Views Asked by At
// Note model 
attributes: {
        // Relations
        notebook: {
            model: 'Notebook'
        },
}

and

// Notebook
attributes: {
        // Relations
        owner: {
            model: 'User'
        },

        notes: {
            collection: 'Note',
            via: 'notebook'
        }
}

in the controller:

        Notebook.findOne({owner: user.id}, function (err, notebook) {
            if (err || !notebook) {
                return res.serverError(err);
            }
// --> until here it goes all fine, finding the Notebook
            Note.find().where({notebook: notebook.id}, function (err, notes) {
                if (err || !notes) {
                    return res.serverError(err);
                }

                return res.json({notebook: notebook, notes: notes});
            })
        })

It is clear that I am trying to get all Notes related to the Notebook. When debugging, I get until the Note.find() and then I don't even enter the callback, so I don't get any results for Note. The err is null, so I wouldn't know if something's wrong.

I am betting that I have set up my model relations wrongly, but it seems correct to me, as from what I have read in tutorials.

P.S. I do have the records in the database, and the ER relations there are setup correctly, because inserting Note records works without problems.

1

There are 1 best solutions below

0
On BEST ANSWER

The models relations seems to be fine.

I think the error come from the fact that there is no callback param in the where method.

Try this instead:

Note
  .find()
  .where({ notebook: notebook.id })
  .exec(function (err, notes) {
    ...
});