无法解析Node js中的sequelize对象

时间:2018-04-11 05:30:53

标签: node.js sequelize.js

以下是使用sequelize ORM

的连接查询的输出
[ dashboard_grid_elements {
dataValues: 
 { id: 1,
   grid_id: '816',
   type: 1,
   subtype: 1,
   created_at: 2018-04-09T13:33:05.776Z,
   updated_at: 2018-04-09T13:33:05.776Z },
_previousDataValues: 
 { id: 1,
   grid_id: '816',
   type: 1,
   subtype: 1,
   created_at: 2018-04-09T13:33:05.776Z,
   updated_at: 2018-04-09T13:33:05.776Z },
_changed: {},
_modelOptions: 
 { timestamps: true,
   validate: {},
   freezeTableName: true,
   underscored: true,
   underscoredAll: false,
   paranoid: false,
   rejectOnEmpty: false,
   whereCollection: null,
   schema: null,
   schemaDelimiter: '',
   defaultScope: {},
   scopes: [],
   indexes: [],
   name: [Object],
   omitNull: false,
   tableName: 'dashboard_grid_elements',
   sequelize: [Object],
   hooks: {},
   uniqueKeys: {} },
_options: 
 { isNewRecord: false,
   _schema: null,
   _schemaDelimiter: '',
   include: undefined,
   includeNames: undefined,
   includeMap: undefined,
   includeValidated: true,
   raw: true,
   attributes: undefined },
__eagerlyLoadedAssociations: [],
isNewRecord: false } ]

当尝试使用dashboard_grid_elements.type获取类型的值时,它显示undefined。有没有办法解析这个json对象。

1 个答案:

答案 0 :(得分:1)

添加var user = {} var mysql = require('mysql') db = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "smsSystem" }) db.connect(err => { if (err) { console.log('ERROR'); process.exit() } console.log('CONNECTED') }) user.signupUser = (jUserData) => { var aData = [ jUserData.txtFirstName, jUserData.txtLastName, jUserData.txtEmail, jUserData.txtMobileNumber ] var stmt = 'INSERT INTO users VALUES ?' db.query(stmt, [aData], (err, jData) => { console.log("jData", jData) if (err) { console.log('BAD, user not inserted') return } else if (jData.affectedRows == 1) { console.log('GREAT, user inserted') } }) } module.exports = user 您的查询,它将返回普通对象。

raw : true

OR(仅当您使用findOne或获取单个对象而不是对象数组时)

model.findOne({
    raw : true ,
    ...
}).then((user) => {
    console.log(user.type);
})