gpt4 book ai didi

node.js - 如何在sequelize中循环结果

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:12 25 4
gpt4 key购买 nike

如何从响应中获取dataValues。我尝试过执行 console.log(result.dataValues) 但它返回未定义。

回应

[ User {
dataValues: {
id: 16,
user_id: '140235016357535420',
server_id: '535881918483398676',
xp: 40995,
coins: 0,
createdAt: 2019-03-09T22
:
59: 09.216Z,
updatedAt: 2019-03-09T22
:
59: 09.216Z
},
},
User {
dataValues: {
id: 16,
user_id: '140235016357535420',
server_id: '535881918483398676',
xp: 40995,
coins: 0,
createdAt: 2019-03-09T22
:
59: 09.216Z,
updatedAt: 2019-03-09T22
:
59: 09.216Z
},
},]

查询

User.findAll({
where: {
server_id: msg.guild.id,
},
limit: 2,
order: [
['xp', 'DESC'],
],
}).then(result => {
console.log(result);
});

型号

'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
user_id: DataTypes.STRING,
server_id: DataTypes.STRING,
xp: DataTypes.INTEGER,
coins: DataTypes.INTEGER,
}, {});
/* User.associate = function(models) {
// associations can be defined here
};*/
return User;
};

最佳答案

这看起来像一个 User 对象数组; dataValues 是每个用户的子级。首先迭代数组并获取对象,然后提取 dataValues

for (let i = 0; i < result.length; i++)  {
console.log(result[i].dataValues);
}

使用forEach():

result.forEach( 
(user) => {
console.log(user.dataValues);
}
);

关于node.js - 如何在sequelize中循环结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com