Javascript 使用rest API中的sequilize从获取用户信息
我正在使用Javascript 使用rest API中的sequilize从获取用户信息,javascript,node.js,rest,Javascript,Node.js,Rest,我正在使用nodejs和sequelize创建一个REST API,我有两个表: 用户表 朋友桌 与 我得到了我所有的朋友列表(存储在我的朋友表中),但我不知道如何(从用户表中)得到他们的名字 这是我获取好友列表的请求: models.Friend.findAll({ where: { $or: [{ UserID: userFound.id }, { idFriend: userFound.id }], status : "a
nodejs
和sequelize
创建一个REST API,我有两个表:
models.Friend.findAll({
where: {
$or: [{
UserID: userFound.id
},
{
idFriend: userFound.id
}],
status : "active"
}
})
在图中,我向您展示了Users表和Friends表
如何在请求中获取朋友的姓名
更新
这是我的用户模型
'use strict';
module.exports = (sequelize, DataTypes) => {
var User = sequelize.define('User', {
email: DataTypes.STRING,
username: DataTypes.STRING,
password: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
isOut: DataTypes.BOOLEAN,
bio: DataTypes.STRING
}, {});
User.associate = function(models) {
// associations can be defined here
models.User.hasMany(models.Message)
models.User.hasMany(models.Friend)
};
return User;
};
这是我的朋友们
'use strict';
module.exports = (sequelize, DataTypes) => {
var Friend = sequelize.define('Friend', {
UserID: DataTypes.INTEGER,
idFriend: DataTypes.INTEGER,
status: DataTypes.STRING
}, {});
Friend.associate = function(models) {
// associations can be defined here
models.Message.belongsTo(models.User, {
foreignKey:{
allowNull: false
}
})
};
return Friend;
};
and my get friends function
showFriend: function (req, res) {
var headerAuth = req.headers['authorization'];
var UserId = jwtUtils.getUserId(headerAuth);
// Params
asyncLib.waterfall([
function (done) {
models.User.findOne({
where: { id: UserId }
})
.then(function (userFound) {
done(null, userFound);
})
.catch(function (err) {
return res.status(500).json({ 'error': 'unable to verify user' });
});
},
function (userFound, TargetFound, done) {
models.Friend.findAll({
where: {
$or: [{
UserID: userFound.id
},
{
idFriend: userFound.id
}],
status : "active"
}
})
.then(function (friends) {
if (friends) {
res.status(200).json(friends);
} else {
res.status(404).json({ "error": "no friends found" });
}
})
.catch(function (err) {
return res.status(500).json({ 'error': 'cannot find Friend' })
})
}
], function (newFriend) {
if (newFriend) {
return res.status(201).json({
'newFriend': newFriend.id
})
} else {
return res.status(500).json({ 'error': 'cannot add Friendss' })
}
});
},
谢谢如果关联了
朋友
和用户
,那么您必须在查询中包含他们:
models.Friend.findAll({
where: {
$or: [
{ UserID: userFound.id },
{ idFriend: userFound.id }
],
status : "active"
},
include: [{
model: models.User
}]
})
然后,您应该能够执行以下操作:
const friends = models.Friend.findAll({ ... })
friends.forEach((friend) => {
/* depends on your Naming Strategy, I'm assuming
`Users` will load as property 'User' on `Friends`,
it depends on how your models and associations are defined
*/
console.log(friend.User.username)
})
如果关联了朋友
和用户
,则必须在查询中包含他们:
models.Friend.findAll({
where: {
$or: [
{ UserID: userFound.id },
{ idFriend: userFound.id }
],
status : "active"
},
include: [{
model: models.User
}]
})
然后,您应该能够执行以下操作:
const friends = models.Friend.findAll({ ... })
friends.forEach((friend) => {
/* depends on your Naming Strategy, I'm assuming
`Users` will load as property 'User' on `Friends`,
it depends on how your models and associations are defined
*/
console.log(friend.User.username)
})
我使用您的建议更新我的问题并提供更多详细信息我包括我的用户模型,但当我拨打showFriend电话时,我得到“无法验证用户”我使用您的建议更新我的问题并提供更多详细信息我包括我的用户模型,但当我拨打showFriend电话时,我得到“无法验证用户”