Categories
JavaScript Answers

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

To check Node Mongoose connection state without creating a new connection, we get the connection property.

For instance, we write

require("./app.js");
const mongoose = require("mongoose");

console.log(mongoose.connection.readyState);

to get the mongoose.connect.readyState property to check if the Mongoose connection is ready.

Categories
JavaScript Answers

How to run multiple commands in package.json with JavaScript?

To run multiple commands in package.json with JavaScript, we use && to separate the commands.

For instance, in package.json, we write

{
  "scripts": {
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "npm run init-assets && npm run init-server"
  }
}

to add the start script that runs the npm run init-assets and npm run init-server commands.

init-assets and init-server are the scripts added before start.

Categories
JavaScript Answers

How to fix ER_NOT_SUPPORTED_AUTH_MODE error on MySQL server?

To fix ER_NOT_SUPPORTED_AUTH_MODE error on MySQL server, we add the user.

For instance, we run

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

to add the root user to the database so we can log in with the user in our apps.

Categories
JavaScript Answers

How to get only dataValues from Node Sequelize ORM?

To get only dataValues from Node Sequelize ORM, we call the findById method.

For instance, we write

const data = await Model.findById(1);
console.log(data.get({ plain: true }));

to call findById to get the item by ID.

And then we call data.get with { plain: true } to return a plain object version of the result.

Categories
JavaScript Answers

How to get full file path in Node.js?

To get full file path in Node.js, we use the path.resolve method.

For instance, we write

const path = require("path");
const absolutePath = path.resolve("./Uploads/MyFile.csv");

to call path.resolve with a relative path to return the corresponding absolute path.