简体   繁体   English

Nextjs 和 Sequelize 模型

[英]Nextjs and Sequelize Models

Hello fellow problem solvers.各位问题解决者您好。 I'm working on rebuilding an older project in Nextjs but currently scratching my head over this issue.我正在努力在 Nextjs 中重建一个旧项目,但目前正在为这个问题抓耳挠腮。

I'm attempting to build an association between an order and it's line items.我正在尝试在订单与其订单项之间建立关联。 Using the associations listed below I'm able to create the correct tables with the expected cols in Postbird but when fetching the data I'm getting the following error:使用下面列出的关联,我能够在 Postbird 中使用预期的列创建正确的表,但是在获取数据时出现以下错误:

Error Message:错误信息:

EagerLoadingError [SequelizeEagerLoadingError]: lineItem is not associated to order!

Here's my associations located in server/index.js:这是我位于 server/index.js 中的关联:

const conn = require("./conn");
const { Artist, LineItem, Order, Track, User, Vinyl } = require("./models");

//ASSOCIATIONS
User.hasMany(Order);
Order.belongsTo(User);

Order.hasMany(LineItem);
LineItem.belongsTo(Order);

Vinyl.hasMany(LineItem);
LineItem.belongsTo(Vinyl);

Vinyl.hasMany(Track);
Track.belongsTo(Vinyl);

Artist.hasMany(Vinyl);
Vinyl.belongsTo(Artist);

module.exports = { conn, Artist, LineItem, Order, Track, User, Vinyl };

And finally the api route最后是 api 路线

import { Order, LineItem, Vinyl, Artist } from "../../../../server/models";
import { requireToken } from "../../../../customMiddleware";

const handler = async (req, res) => {
  if (req.method === "GET") {
    try {
      const userOrders = await Order.findAll({
        where: { userId: req.query.id },
        include: {
          model: LineItem,
          attributes: ["id", "qty"],
          include: {
            model: Vinyl,
            attributes: ["id", "name", "stock", "price", "img"],
            include: {
              model: Artist,
              attributes: ["id", "name"],
            },
          },
        },
      });
      userOrders.sort((a, b) => a.id - b.id);
      res.status(200).json({ success: true, userOrders });
    } catch (error) {
      console.log(error);
      res.status(500).json({
        success: false,
        message: `An error has occurred. Unable to fetch user order id# ${req.query.id}.`,
        error,
      });
    }
  }
};

export default requireToken(handler);

I'd also like the mention that of course, this is currently working fine on the old project so I'm a little puzzled over this.当然,我还想提一下,这目前在旧项目上运行良好,所以我对此有点困惑。

Any suggestions would be appreciated!任何建议,将不胜感激!

I've tried building the associations in different orders but nothing changed.我试过以不同的顺序建立关联,但没有任何改变。

You imported models directly from server/models folder where you always get new model definitions without associations.您直接从server/models文件夹导入模型,您总是在其中获得没有关联的新 model 定义。 You need to import them from server/index module:您需要从server/index模块导入它们:

import { Order, LineItem, Vinyl, Artist } from "../../../../server/index";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM