如何从响应中获取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);
}
);
我是一名优秀的程序员,十分优秀!