Saturday, 22 April 2017

Can't retrieve data from associates in Sequelize

I'm pretty new to NodeJS in general and even new to Sequelize. As an exercise, I'm trying to convert a .NET Core application I have while maintaining the SQL schema I have. To do this, I started with the Sequelize / Express tutorial they had on their website. From there, I read through the documentation and created several models to work with that match my .NET app.

Building

module.exports = function (sequelize, DataTypes) {

var Building = sequelize.define('Building', {
    BuildingId: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    Name: {
        type: DataTypes.STRING,
        allowNull: false
    }
}, {
    classMethods: {
        associate: function (models) {
            Building.belongsTo(models.Campus, {
                foreignKey: 'CampusId'
            });

            ....
        },
        getByCampus: function (models, campusId) {
            this.findAll({
                include: [{
                    model: models.Campus,
                    where: {
                        CampusId: campusId
                    }
                }]
            }).catch(function (err) {
                return Promise.reject(err); // This doesn't seem to get passed to express?
            });

            ....

Campus

module.exports = function (sequelize, DataTypes) {
'use strict';

var Campus = sequelize.define('Campus', {
    CampusId: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    Enabled: {
        type: DataTypes.BOOLEAN,
        defaultValue: true,
        allowNull: false
    },
    Name: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
    }
}, {
    classMethods: {
        associate: function (models) {
            Campus.hasMany(models.Building, {
                as: 'Buildings',
                onDelete: 'CASCADE',
                foreignKey: 'CampusId'
            });

            ...

Route

router.get('/:id/building', function (req, res, next) {
    var campusId = parseInt(req.params.id);
    models.Building.getByCampus(models, campusId).then(function (buildings) {
        res.send(buildings);
    }).catch(function (err) {
        res.send(err);
    });
});

What I'd like is:

  • To be able to access the Campus from an instance of Building
  • Be able to access Buildings from an instance of Campus
  • Be able to get a single Building from my Building model by CampusId

What I'm getting is:

Executing (default): SELECT Building.BuildingId, Building.Name, Building.createdAt, Building.updatedAt, Building.CampusId, Campus.CampusId AS Campus.CampusId, Campus.Enabled AS Campus.Enabled, Campus.Name AS Campus.Name, Campus.createdAt AS Campus.createdAt, Campus.updatedAt AS Campus.updatedAt FROM Buildings AS Building INNER JOIN Campuses AS Campus ON Building.CampusId = Campus.CampusId AND Campus.CampusId = 1; GET /api/v1/campus/1/building 500 10.639 ms - 1968

It's completely ignoring my catch in Building.getByCampus, though if I set a break point, I can confirm I'm getting to the function. What dumb thing am I missing? I've spend several hours trying different things and re-reading their docs and feel like I'm hitting a wall.



via jackmusick

No comments:

Post a Comment