I'm trying Objection.js, everything works pretty nice except the eager, I can't make it works. It's in a simple User/Post configuration. So this is my post model:
class Post extends Model {
static get tableName() {
return 'posts';
}
static get relationMappings() {
const User = require('./User');
return {
author: {
relation: Model.BelongsToOneRelation,
modelClass: User,
join: {
from: 'posts.author_id',
to: 'users.id'
}
}
};
};
}
And this is my controller:
exports.show = function (req, res) {
Post.query()
.eager('author') // Load the user
.findById(req.params.post_id)
.then(post => {
if (!post)
res.json({code: 23});
else
res.json(post);
})
.catch(err => {
res.json({code: 31, message: err.sqlMessage});
});
};
All I get is {code: 31} 馃槥. Can someone help me?
eager-loading definitely works, so there's something else wrong here.
Could you create a minimal reproducible example?
Also, can you add .debug() to your query and post whatever gets outputted in stdout?
If you don't have time to create a reproducible example, please at least provide the error message and .debug() output as @fl0w suggested.
There is no way for us to help you without any information about what failed. As @fl0w mentioned, this not the case of eager being broken. It is being successfully used by hundreds of people and it has hundreds of tests that I run daily.
I never said it was a problem due to the eager function, it's just I didn't understood how to use it ! 馃槈
I found the problem, I forggot a module.exports = Group in another model used by the user.
Great that you found the problem. If someone opens an issue and provides no error messages or stack traces and very little code I easily think they are assuming the problem is in objection and not in their code. I assume their reasoning is something like "he/she can easily reproduce the error by running this one line of code" 馃槃.
I'm always happy to help out but in the future please provide possible error messages, .debug() outputs and anything you think could be relevant or could help other people to figure out what's happening.
Most helpful comment
Great that you found the problem. If someone opens an issue and provides no error messages or stack traces and very little code I easily think they are assuming the problem is in objection and not in their code. I assume their reasoning is something like "he/she can easily reproduce the error by running this one line of code" 馃槃.
I'm always happy to help out but in the future please provide possible error messages,
.debug()outputs and anything you think could be relevant or could help other people to figure out what's happening.