javascript - Sequelize.js findAll 与包含搜索外键使页面永远不会加载

标签 javascript node.js sequelize.js

说明

我有一个由客户、地址和订单组成的简单数据库。我正在尝试查找地址为某个字符串的所有订单。例如,我可以提供城市或州,我想根据它进行查找。

订单表:
enter image description here

地址表:
enter image description here

订单模型:

module.exports = db.define('orders', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    repId: { type: Sequelize.INTEGER },
    totalItemCost: { type: Sequelize.DECIMAL(10,2), allowNull: false},
    shippingCost: { type: Sequelize.DECIMAL(10,2), allowNull: false},
    orderDate: { type: Sequelize.DATE, allowNull: false},
    isPaid: { type: Sequelize.BOOLEAN, allowNull: false},
    taxPercentage: { type: Sequelize.DECIMAL(10,4), allowNull: false},
}, {timestamps: false, freezeTableName: true, tableName: 'Orders'});

地址模型:
module.exports = db.define('address', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    firstName: { type: Sequelize.STRING, allowNull: false},
    lastName: { type: Sequelize.STRING, allowNull: false},
    city: { type: Sequelize.STRING, allowNull: false},
    address: { type: Sequelize.STRING(256), allowNull: false},
    zip: { type: Sequelize.STRING(10), allowNull: false},
}, {timestamps: false, freezeTableName: true, tableName: 'Address'});

关系:
var models = {};
models.Address = require("./models/address.js");
models.Customer = require("./models/customer.js");
models.Orders = require("./models/orders.js");

// Orders Relations
models.Orders.belongsTo(models.Customer, { foreignKey: { name: 'customerId', allowNull: false }});
models.Orders.belongsTo(models.Address, { foreignKey: { name: 'shippingAddressId', allowNull: false }});

问题

例如,假设我想查找城市为 Birchwood 的所有订单。我在我的 findAll 命令中使用 include ,模型设置为我的地址,因为我专门命名了我的送货地址,设置了“as”语句。这导致页面永远不会加载,我不确定我做错了什么。如果我删除 include 命令,我的所有订单都可以正常加载。当使用“belongsTo”时,我没有看到任何包含的示例。当使用“hasMany”时,它看起来像是使用“association”而不是“foreign_key”。这就是为什么“as”不起作用的问题吗?
models.Orders.findAll({
    where: where,
    include: [{
        model: models.Address, 
        as: 'shippingAddressId',
    }] 
}).then(function(orders) { 
    res.json(orders);
}).catch(function(err) {
    res.status(500).json({"error": err});
});

编辑 1

我为我的查询语句添加了错误处理,但现在只输出 {"error":{}}。有错误是空白的,所以这不是很有帮助。如果我删除 "as: 'shippingAddressId'"行,则模型会加载,并将地址放在不需要的 json 结构中的“地址”字段下。添加一个 where 子句会导致它返回一个空对象 {}。我基本上想要包括:[{ all: true,nested: true }] 但也过滤了关系地址。

最佳答案

显然查询中有错误。这就是 res.json(orders); 从未调用过的原因。我在你的代码中没有看到错误处理。看看 http://bluebirdjs.com/docs/api/catch.html 。当你捕捉到错误时,输出它并查看到底发生了什么。

关于javascript - Sequelize.js findAll 与包含搜索外键使页面永远不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298606/

相关文章:

javascript - 在元素上加载

javascript - For 循环内的 If/Else 语句所需的语法

node.js - node.js 应该监听哪些端口?如何以及为什么?

sequelize.js - Sequelize js自动递增主键传递空值

javascript - 使用 Passport.js 和 Sequelize 引用多个模型

javascript - 从 symfony Controller 返回 json 数组,以便在 highcharts.js 上使用

javascript - 查询 :focus when the browser ain't focused

javascript - AngularJS, ng-view + css3 keyframes with jade template - ng-cloak 不工作

node.js - 为什么 zlib.deflate(buf,callback) 是异步的?

node.js - 在graphql中获取复杂对象的字段类型错误