0
我正在使用Angular和Nodejs構建的應用程序進行sequelize。我已經構建了一個用戶模型,其中有許多通知作爲發件人,並且有許多通知作爲接收者。通知模型也屬於「用戶」作爲發件人,「belongsTo」用戶爲「接收者」。我的問題是,當我嘗試在通知上實現GET http請求時,我收到Sequelize Eager加載錯誤。我已經註釋掉了include語句以及用戶的通知屬性,並且GET請求起作用。我只是不明白是什麼導致了錯誤。Sequelize Eeager加載錯誤 - 無法弄清楚包括
這是我的模特。
「use strict」;
module.exports = function(sequelize, DataTypes) {
var Notification = sequelize.define("Notification", {
startAddress: DataTypes.STRING,
endAddress: DataTypes.STRING,
tripDate: DataTypes.DATE,
tripHour: DataTypes.INTEGER,
numberOfPassengers: DataTypes.INTEGER,
price: {
type: DataTypes.INTEGER,
defaultValue: -1
}
});
Notification.associate = function(models) {
Notification.belongsTo(models.User, {
as: "sender",
onDelete: "CASCADE",
foreignKey: "senderId"
});
Notification.belongsTo(models.User, {
as: "receiver",
onDelete: "CASCADE",
foreignKey: "receiverId"
});
};
return Notification;
};
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
email: DataTypes.STRING,
password: DataTypes.STRING,
profileImgUrl: DataTypes.STRING,
fName: DataTypes.STRING,
lName: DataTypes.STRING,
yearOfBirth: DataTypes.INTEGER,
gender: DataTypes.STRING,
ratings: {
type: DataTypes.ARRAY(DataTypes.INTEGER),
defaultValue: [5]
},
createdAt: {
type: DataTypes.DATE,
defaultValue: sequelize.literal('NOW()')
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: sequelize.literal('NOW()')
}
});
User.associate = function(models) {
User.hasMany(models.RideRequest, {
onDelete: "CASCADE",
foreignKey: "riderId"
});
User.hasMany(models.DriveRequest, {
onDelete: "CASCADE",
foreignKey: "driverId"
});
User.hasMany(models.Notification, {
as: 'sender',
onDelete: "CASCADE",
foreignKey: "senderId"
});
User.hasMany(models.Notification, {
as: 'receiver',
onDelete: "CASCADE",
foreignKey: "receiverId"
});
};
return User;
};
這裏是我的通知控制器,它適用於通知路由邏輯。
list: function list(req,res) {
var decoded = jwt.decode(req.query.token);
return model.User.findOne({
where: {id: decoded.user.id},
include: [{
model: model.Notification
}]
}).then(function (user) {
let ratingAv;
for (let rating of user.ratings) {
ratingAv = (user.ratings.length/(rating += rating)*10);
}
console.log(ratingAv + ' RATINGSSSS');
let resObj = Object.assign({}, {
id: user.id,
profileImgUrl: user.profileImgUrl,
fName: user.fName,
lName: user.lName,
yearOfBirth: user.yearOfBirth,
gender: user.gender,
ratings: ratingAv,
notifications: user.Notifications.map(function(notification) {
return Object.assign({}, {
id: notification.id,
startAddress: notification.startAddress,
endAddress: notification.endAddress,
tripDate: notification.tripDate,
tripHour: notification.tripHour,
numberOfPassengers: notification.numberOfPassengers,
price: notification.price,
senderId: notification.senderId,
receiverId: notification.receiverId
});
})
});
return res.status(201).json({
message: 'Notifications retrieved successfully',
obj: resObj
});
}).catch(function (err) {
return res.status(400).json({
title: 'There was an error retrieving notifications',
error: err
});
});
},
如果我甚至能以某種方式得到一個更好的錯誤,我可以弄明白。這個錯誤是非常普遍的,並且不能幫助我理解可能拼寫錯誤或放錯位置等問題。任何人都可以找出問題所在?