Categories
JavaScript Answers

How to copy to clipboard in Node.js?

Sometimes, we want to copy to clipboard in Node.js.

In this article, we’ll look at how to copy to clipboard in Node.js.

How to copy to clipboard in Node.js?

To copy to clipboard in Node.js, we can use the clipboardy package.

To install it, we run

npm i clipboardy

Then we run it by writing

const clipboardy = require('clipboardy');

clipboardy.writeSync('hello');
clipboardy.readSync();

to call writeSync with the string we want to copy to the clipboard.

Then we read the clipboard content with readSync.

Conclusion

To copy to clipboard in Node.js, we can use the clipboardy package.

Categories
JavaScript Answers

How to use Node.js connect MemoryStore in production?

Sometimes, we want to use Node.js connect MemoryStore in production.

In this article, we’ll look at how to use Node.js connect MemoryStore in production.

How to use Node.js connect MemoryStore in production?

To use Node.js connect MemoryStore in production, we can use the cookie-session package.

To install it, we run

npm install cookie-session

Then we use it in our Connect app by writing

app.use(require('cookie-session')({
  //  config
}));

to require the cookie-session module and call it with some options store in the object.

And then we call app.use with the returned middleware to let us use cookie-session as the session store.

Conclusion

To use Node.js connect MemoryStore in production, we can use the cookie-session package.

Categories
JavaScript Answers

How to require file as string with Node.js?

Sometimes, we want to require file as string with Node.js.

In this article, we’ll look at how to require file as string with Node.js.

How to require file as string with Node.js?

To require file as string with Node.js, we can use fs.readFileSync.

For instance, we write

const fs = require('fs')
const path = require('path')

const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')

to call fs.readFileSync to read the file at path returned by path.resolve(__dirname, 'email.css').

We specify to read the file as a 'utf8' encoded text file.

readFileSync reads the file synchronously just like require.

Conclusion

To require file as string with Node.js, we can use fs.readFileSync.

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.