Question

Stumbling around for a few hours now with Sequelize for Node.JS. I had no such luck using it to set up associations (I'd have to tear apart most of my architecture to do so, on limited time). For my app, there are only a few associations (blog related mostly).

In one of my blog API calls (this is for a specific blog ID):

exports.single = function (req, res, next) {
db.Models.blog.find({
    where: res.locals.where
})
.success(function (blog) {
    getAuthors(blog.BLOG_ID, function (authors) {
        blog.AUTHORS = authors;
        res.locals.data = blog;
        next();
    });
})
.error(function (err) {
    res.locals.error = err;
    next();
});

}

I'm calling getAuthors(blogId) to grab all of the authors in the Author <-> Blog linking table:

function getAuthors (blogId, fn) {
db.Models.author.findAll({
    where: { BLOG_ID: blogId }
})
.success(function (authors) {
    fn(authors);
})
.error (function (err) {
    fn([]);
});

}

It works, mostly. I'm getting the correct data back, but I'm trying to merge that in with the original blog data. Preferably as a property.

Was it helpful?

Solution

After dumping the contents of my authors object coming back from the second query, I learned of a property used when pulling the data from a Sequelize object

model.attritubes[];

Is an array of Strings which lists which data attributes will be outputted from the Sequelize Object. So before my line:

blog.AUTHORS = authors;

I did:

blog.attributes.splice(5, 0, 'AUTHORS');

I am not a clever man.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top