Shaju Nr
Shaju Nr

Reputation: 358

Unable to parse a sequelize object in Node js

Following is the output of join query using 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 } ]

When trying to get the value of type using dashboard_grid_elements.type,it shows undefined.Is there any way to parse this json object.

Upvotes: 0

Views: 339

Answers (2)

kush
kush

Reputation: 645

Course.findOne({raw : true ,where: {
           course_slug:slug,
           status:responseCode.STATUS_ACTIVE
       },attributes: ['id','course_title','course_slug','description','age_group','image','no_of_student_in_class',
           'class_duration','no_of_classes','per_class_fee','is_certificate','is_course_upcoming','what_we_learn','features']}).then(course_detial =>{
       result(null,course_detial);
   }).catch(err =>{
       console.log(err)
   });

Upvotes: 0

Vivek Doshi
Vivek Doshi

Reputation: 58553

Add raw : true your query , it will return plain object .

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

OR (Only if you are using findOne or getting single object not array of object)

model.findOne({
    ...
}).then((user) => {
    user = user.toJSON();
    console.log(user.type);
})

Upvotes: 2

Related Questions