Javascript Sequelize.js findAll和include搜索外键使页面永远不会加载
说明 我有一个简单的数据库,由客户、地址和订单组成。我试图找到地址是字符串的所有订单。例如,我可以提供城市或州,我想基于此进行查找 订单表: 地址表: 订单型号:Javascript Sequelize.js findAll和include搜索外键使页面永远不会加载,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,说明 我有一个简单的数据库,由客户、地址和订单组成。我试图找到地址是字符串的所有订单。例如,我可以提供城市或州,我想基于此进行查找 订单表: 地址表: 订单型号: module.exports = db.define('orders', { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, repId: { type: Sequ
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 }});
地址模型:
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 }});
关系:
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 }});
问题
比如说,我想找到城市是桦木的所有订单。我在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结构中不需要的字段“address”下。将where子句添加到将导致其返回空对象{}。我基本上希望include:[{all:true,nested:true}],但也要对关系地址进行筛选。显然查询中有错误。这就是为什么
res.json(订单)代码>从未调用。我看不到代码中的错误处理。看一看。当您发现错误时,请将其输出并查看到底发生了什么。我通过将关联添加到关系中解决了此问题:
// Orders Relations
models.Orders.belongsTo(models.Customer, { as: 'customer', onDelete: 'CASCADE', foreignKey: { name: 'customerId', allowNull: false }});
models.Orders.belongsTo(models.Address, { as: 'shippingAddress', onDelete: 'CASCADE', foreignKey: { name: 'shippingAddressId', allowNull: false }});
models.Orders.belongsTo(models.PaymentMethod, { as: 'paymentMethod', onDelete: 'CASCADE', foreignKey: { name: 'paymentMethodId', allowNull: false }});
models.OrderItem.belongsTo(models.Orders, { as: 'order', onDelete: 'CASCADE', foreignKey: { name: 'orderId', allowNull: false }});
然后在我的查询中使用该关联名称:
models.Orders.findAll({
where: where,
include: [{
model: models.Address,
as: 'shippingAddress',
where: {
city: {like: "%Wedgewood%" }
}
}]
}).then(function(orders) {
res.json(orders);
}).catch(function(err) {
res.status(500).json({"error": err});
});
我觉得这种联系是自动建立的。我更新了我的问题。错误为空,但是删除我的“as”语句会导致数据以不同的列名加载代码><代码>错误
必须包含堆栈跟踪和错误消息。即使这个问题已经解决,我仍然建议您在调试器中调试代码。看看那里显示了什么错误。并且至少找到将错误打印到控制台的方法。像console.dir(err)