Sequelize渴望加载返回未定义的数组

时间:2019-04-26 11:36:19

标签: mysql node.js sequelize.js pug eager-loading

当我尝试使用紧急加载来获取与订单相关的产品时,我得到一个数组,当我尝试使用pug遍历该数组时,该数组显示为未定义。

我已经尝试查看文档并在线找到解决方案,但我尚未解决

exports.getOrders = (req, res, next) => {
    req.user.getOrders({include: ['products']})
    .then((orders) => {
        console.log(orders);
        res.render("shop/orders", {
            docTitle: "Orders",
            path: "/orders",
            orders: orders,
            hasOrders: orders.length > 0 ? true: false
        });
    })
    .catch((err) => {
        console.log(err);
    }) 
}

这是用于检索订单和相关产品的控制器

extends ../layout.pug

block content
    if hasOrders
        h1 You have orders!!!
        ul
            each val in orders
                li
                    h1 #{orders.id}
                    ul
                        each val in orders.products
                            li #{val.title} ( #{val.quantity} )
    else
        h1 No orders to show!!!

这是通过输入的数据呈现视图的pug文件

[ order {
    dataValues:
     { id: 1,
       createdAt: 2019-04-26T11:24:20.000Z,
       updatedAt: 2019-04-26T11:24:20.000Z,
       userId: 1,
       products: [Array] },
    _previousDataValues:
     { id: 1,
       createdAt: 2019-04-26T11:24:20.000Z,
       updatedAt: 2019-04-26T11:24:20.000Z,
       userId: 1,
       products: [Array] },
    _changed: {},
    _modelOptions:
     { timestamps: true,
       validate: {},
       freezeTableName: false,
       underscored: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: [Object],
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: {},
       indexes: [],
       name: [Object],
       omitNull: false,
       sequelize: [Sequelize],
       hooks: {} },
    _options:
     { isNewRecord: false,
       _schema: null,
       _schemaDelimiter: '',
       include: [Array],
       includeNames: [Array],
       includeMap: [Object],
       includeValidated: true,
       attributes: [Array],
       raw: true },
    isNewRecord: false,
    products: [ [product] ] } ]
TypeError: C:\Users\garry\Documents\Udemy\Node js\Project\views\shop\orders.pug:11
    9|                     h1 #{orders.id}
    10|                     ul
  > 11|                         each val in orders.products
    12|                             li #{val.title} ( #{val.quantity} )
    13|     else
    14|         h1 No orders to show!!!

Cannot read property 'length' of undefined

我觉得我可能会缺少一些明显的东西,任何帮助将不胜感激。

谢谢。

0 个答案:

没有答案
相关问题