Possible error: sequelize multiple nested includes?

I am using a simple mysql database for tracking miles driven and time logged for work. I have a table of Users; a table of Weeks (each Week linked to a User); a table of Days (each Day linked to a Week); and a table of Miles and a table of Hours (each of these linked to a Day).

When I get data for a User, everything comes back nicely…until I have an entry for Hours. In my resolver, I get an error, ending_time is not defined. This error comes not when I do my query, but when I use sequelize’s get function to extract the data from the query’s returned object.

I am running this application in Node, with a GraphQL interface to my MySQL database. I am running node version 18.19.1, MySQL version 8.0.36, and Sequelize version 6.37.2. Here are the various code snippets:

First, the sequelize model for Hours:

const { Model, DataTypes } = require("sequelize");
const sequelize = require("../config/connection");
class Hours extends Model {}

Hours.init(
  {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    day_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: "day",
        key: "id",
      },
    },
    starting_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
    },
    ending_time: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
    },
    total_minutes: {
      type: DataTypes.VIRTUAL,
      get() {
        return ending_time && starting_time ? ending_time - starting_time : 0;
      },
    },
  },
  {
    sequelize,
    timestamps: true,
    freezeTableName: true,
    underscored: true,
    modelName: "hours",
  }
);

module.exports = Hours;

(though I’ve named it “Hours” I recently decided to chuck time types and just do it easier: starting_time and ending_time are integers representing minutes from midnight)

This is exactly the same model as my Miles model. Same DataTypes, same default values, just different field naming.

The relationships are set up here:

const User = require("./User");
const Week = require("./Week");
const Day = require("./Day");
const Miles = require("./Miles");
const Hours = require("./Hours");

User.hasMany(Week, {
  foreignKey: "user_id",
});

Week.belongsTo(User, {
  foreignKey: "user_id",
});

Week.hasMany(Day, {
  foreignKey: "week_id",
});

Day.belongsTo(Week, {
  foreignKey: "week_id",
});

Day.hasMany(Miles, {
  foreignKey: "day_id",
});

Miles.belongsTo(Day, {
  foreignKey: "day_id",
});

Day.hasMany(Hours, {
  foreignKey: "day_id",
});

Hours.belongsTo(Day, {
  foreignKey: "day_id",
});

module.exports = { User, Week, Day, Miles, Hours };

Within my resolver, here is the GetUser query:

getUser: async (parent, { id }) => {
  const userData = await User.findOne({
    where: { id },
    include: [
      {
        model: Week,
        include: [
          {
            model: Day,
            include: [{ model: Miles }, { model: Hours }],
            separate: true,
          },
        ],
      },
    ],
    order: [[{ model: Week, as: "weeks" }, "start_date", "DESC"]],
  });

  const user = await userData.get({ plain: true });

  return {
    user_name: user.user_name,
    created_at: user.createdAt,
    weeks: user.weeks,
  };
},

You can see I and finding the User off the their id, including the User’s Weeks, and those Weeks’ Days, and those Days’ Miles and Hours.

If there are no Hours associated with this User, then I get the error ending_time is not defined. Using some console.logs I know that the error is not coming until const user = await userData.get({ plain: true }). If there are no Hours, then I do not get the error.

Looking at my tables directly in MySQL, I see no difference between show fields from miles and show fields from hours, except for field names (“starting_time” vs. “start_miles”, “ending_time vs. “ending_miles). When I do select * from hours; I see exactly what I expect, a record with an id, a day_id, a starting_time of 0 and an ending_time of 0.

I originally had the field names as start_time and end_time, and when the error came up end_time not defined I thought maybe the field name “end_time” was somehow reserved and I shouldn’t use it…but when I changed the field to “ending_time” I got the same error with the new field name.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật