Parth Kharecha
Parth Kharecha

Reputation: 6503

in Sequelize after create add in response "val": "CURRENT_TIMESTAMP" instance of time(2019-03-22 09:56:38)

In Sequelize after create add in response "val": "CURRENT_TIMESTAMP" instance of time(2019-03-22 09:56:38) what define in code is described below

model:

        created_at: {
              type: DataTypes.DATE,
              allowNull: false,
              defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
            },

controller:

    Message.create(
          {
            col: body.col
          },
        ).then((
          const MessageResponse = response.get({
            plain: true,
          });
       res.send(apiSuccessHandler({}, { MessageResponse, message: 'Your request submitted successfully.' }, 200));
    }).catch((err) => {
      console.log(err);
      res.status(403).send(apiFailureHandler({ message: "We couldn't save your request, please try again." }, {}, 403));
    });

response:

{
    "resultState": {
        "status": 200,
        "message": "Success"
    },
    "MessageResponse": {
        //what i get
         "created_at": {
            "val": "CURRENT_TIMESTAMP"
        },
        //my requirement 
        "created_at": 2019-03-22 09:56:38,
        "id": 60,

    },
    "message": "Your request submitted successfully."
}

Upvotes: 3

Views: 1667

Answers (3)

Renish Gotecha
Renish Gotecha

Reputation: 2522

You can set defaultValue : DataTypes.NOW in a model like below

createdDateTime: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
},

Alternatively, You can use moment()

createdDateTime: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: moment()
},

Upvotes: 0

Kathak Dabhi
Kathak Dabhi

Reputation: 399

You should not use the defaultValue: sequelize.literal('CURRENT_TIMESTAMP') in model definition instead it should be in your migration file where you define the table structure. in migration file:

createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
}

sequelize.literal creates a object representing a literal, i.e. something that will not be escaped.

And when you use sequelize.literal('CURRENT_TIMESTAMP') in model definition it creates an object with {val: 'CURRENT_TIMESTAMP'} and assign it to model field. So when you create an model its created_at field contain the literal object and not Date value. and due to that it is returning incorrect value for created_at field.

If you want to pass the default value to your model definition than it should contain the value of the Date directly.

ex: in Model:

created_at: {
              type: DataTypes.DATE,
              allowNull: false,
              defaultValue: moment().format('YYYY-MM-DDTHH:mm:s:ss') + '.000Z',
            },

Upvotes: 2

Mayuri Patel
Mayuri Patel

Reputation: 41

You can try this

 Message.create(
      {
        col: body.col,
        created_at: moment(), // add this line
      },
    ).then((
      const MessageResponse = response.get({
        plain: true,
      });
   res.send(apiSuccessHandler({}, { MessageResponse, message: 'Your request submitted successfully.' }, 200));
}).catch((err) => {
  console.log(err);
  res.status(403).send(apiFailureHandler({ message: "We couldn't save your request, please try again." }, {}, 403));
});

Upvotes: 4

Related Questions