Categories
JavaScript Answers

How to use async and await with Node.js AWS SDK?

Sometimes, we want to use async and await with Node.js AWS SDK.

In this article, we’ll look at how to use async and await with Node.js AWS SDK.

How to use async and await with Node.js AWS SDK?

To use async and await with Node.js AWS SDK, we can call the promise method after we call the AWS SDK method.

For instance, we write

try {
  const key = await kms.generateDataKey().promise();
} catch (e) {
  console.log(e);
}

to call promise after call generateDataKey to return a promise with the resolved value assigned to key.

Conclusion

To use async and await with Node.js AWS SDK, we can call the promise method after we call the AWS SDK method.

Categories
JavaScript Answers

How to retrieve the last inserted ID with Node.js MySQL?

Sometimes, we want to retrieve the last inserted ID with Node.js MySQL.

In this article, we’ll look at how to retrieve the last inserted ID with Node.js MySQL.

How to retrieve the last inserted ID with Node.js MySQL?

To retrieve the last inserted ID with Node.js MySQL, we can get it from the callback that’s called when the query is done.

For instance, we write

connection.query('INSERT INTO posts SET ?', {
  title: 'test'
}, (err, result, fields) => {
  if (err) {
    throw err;
  }
  console.log(result.insertId);
});

to call connection.query to make a INSERT query.

We call query with a callback and get the ID of the inserted entry with result.insertId.

Conclusion

To retrieve the last inserted ID with Node.js MySQL, we can get it from the callback that’s called when the query is done.

Categories
JavaScript Answers

How to authenticate Supertest requests with Passport?

Sometimes, we want to authenticate Supertest requests with Passport.

In this article, we’ll look at how to authenticate Supertest requests with Passport.

How to authenticate Supertest requests with Passport?

To authenticate Supertest requests with Passport, we can make a request to log in.

For instance, we write

const request = require('superagent');

const user1 = request.agent();
user1
  .post('http://localhost:4000/signin')
  .send({
    user: 'abc@example.com',
    password: 'password'
  })
  .end((err, res) => {
    // ...
  });

to call post with the login endpoint URL.

And we call send with a JSON object with the user and password parameters accepted by the login endpoint.

Finally, we call end with a callback to get the response from res.

Conclusion

To authenticate Supertest requests with Passport, we can make a request to log in.

Categories
JavaScript Answers

How to set object ID as a data type in Node.js Mongoose?

Sometimes, we want to set object ID as a data type in Node.js Mongoose.

In this article, we’ll look at how to set object ID as a data type in Node.js Mongoose.

How to set object ID as a data type in Node.js Mongoose?

To set object ID as a data type in Node.js Mongoose, we can use Schema.ObjectId as the type.

For instance, we write

const mongoose = require('mongoose');
const {
  Schema
} = mongoose;
const {
  ObjectId
} = Schema;

const SchemaProduct = new Schema({
  categoryId: ObjectId,
  title: String,
  price: Number
});

to use the Schema constructor to create a new schema.

And we set the categoryId field to the ObjectId type by setting categoryId to ObjectId.

Conclusion

To set object ID as a data type in Node.js Mongoose, we can use Schema.ObjectId as the type.

Categories
JavaScript Answers

How to check Node.js Mongoose connection state without creating a new connection?

Sometimes, we want to check Node.js Mongoose connection state without creating new connection.

In this article, we’ll look at how to check Node.js Mongoose connection state without creating new connection.

How to check Node.js Mongoose connection state without creating a new connection?

To check Node.js Mongoose connection state without creating new connection, we can use the mongoose.connection.readyState property.

For instance, we write

const mongoose = require('mongoose');

console.log(mongoose.connection.readyState);

The possible values for mongoose.connection.readyState include:

  • 0: disconnected
  • 1: connected
  • 2: connecting
  • 3: disconnecting

Conclusion

To check Node.js Mongoose connection state without creating new connection, we can use the mongoose.connection.readyState property.