如何从响应中删除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);
}
);