Categories
JavaScript Answers

How to fix “Unknown column ‘*.createdAt’ in ‘field list'” in Sequelize?

Sometimes, we want to fix "Unknown column ‘*.createdAt’ in ‘field list’" in Sequelize.

In this article, we’ll look at how to fix "Unknown column ‘*.createdAt’ in ‘field list’" in Sequelize.

How to fix "Unknown column ‘*.createdAt’ in ‘field list’" in Sequelize?

To fix "Unknown column ‘*.createdAt’ in ‘field list’" in Sequelize, we should make sure the created_at column is in the database table and the createAt property is in the model.

For instance, we write

const users = sequelize.define('users', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  createdAt: {
    field: 'created_at',
    type: Sequelize.DATE,
  },
  updatedAt: {
    field: 'updated_at',
    type: Sequelize.DATE,
  },
  //...
})

to call sequelize.define to define the 'users' model.

In it, we add the createdAt column and set the column type to Sequelize.DATE.

And we map the field to the created_at column in the users table.

Conclusion

To fix "Unknown column ‘*.createdAt’ in ‘field list’" in Sequelize, we should make sure the created_at column is in the database table and the createAt property is in the model.

Categories
JavaScript Answers

How to fix Mongoose findByIdAndUpdate not returning correct value?

Sometimes, we want to fix Mongoose findByIdAndUpdate not returning correct value.

In this article, we’ll look at how to fix Mongoose findByIdAndUpdate not returning correct value.

How to fix Mongoose findByIdAndUpdate not returning correct value?

To fix Mongoose findByIdAndUpdate not returning correct value, we call findByIdAndUpdate with the new option set to true.

For instance, we write:

Model.findByIdAndUpdate(id, updateObj, {
  new: true
}, (err, model) => {
  //...
})

to call findByIdAndUpdate with the id of the item to update, updateObj with the new data for the item, an object with new set to true, and the callback that runs when the update is done.

We set new to true to return the new document value as the value of model in the callback.

Conclusion

To fix Mongoose findByIdAndUpdate not returning correct value, we call findByIdAndUpdate with the new option set to true.

Categories
JavaScript Answers

How to delete items with Sequelize.js?

Sometimes, we want to delete items with Sequelize.js.

In this article, we’ll look at how to delete items with Sequelize.js.

How to delete items with Sequelize.js?

To delete items with Sequelize.js, we can use the destroy method.

For instance, we write

Model.destroy({
  where: {
    // ...
  }
})

to call Model.destroy with an object that has the where property to specify which items we want to delete.

Conclusion

To delete items with Sequelize.js, we can use the destroy method.

Categories
JavaScript Answers

How to get all count of Mongoose model?

Sometimes, we want to get all count of Mongoose model.

In this article, we’ll look at how to get all count of Mongoose model.

How to get all count of Mongoose model?

To get all count of Mongoose model, we can use the model count method.

For instance, we write

userModel.count({}, (err, count) => {
  console.log("Number of users:", count);
})

to call useModel.count to get the count of the items in the collection mapped to userModel.

We get the count from the count parameter in the callback.

Conclusion

To get all count of Mongoose model, we can use the model count method.

Categories
JavaScript Answers

How to do batch insert with Node.js Mongoose?

Sometimes, we want to do batch insert with Node.js Mongoose.

In this article, we’ll look at how to do batch insert with Node.js Mongoose.

How to do batch insert with Node.js Mongoose?

To do batch insert with Node.js Mongoose, we can use the insertMany method.

For instance, we write

const rawDocuments = [
  /* ... */
];

const mongooseDocuments = await Book.insertMany(rawDocuments)

to call Book.inseertMany with the rawDocuments array with the plain objects we want to insert as documents into the books collection.

Then we get the inserted documents from the resolved value of the returned promise as an array and assign that to mongooseDocuments.

Conclusion

To do batch insert with Node.js Mongoose, we can use the insertMany method.