Categories
JavaScript Answers

How to fix the ‘Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused’ error with Node MongoDB?

To fix the ‘Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused’ error with Node MongoDB, we remove the Mongo lock file.

To do this, we run

sudo rm /var/lib/mongodb/mongod.lock
sudo service mongod restart

to remove the mongod.lock file with rm.

Then we restart the Mongo server with service mongod restart.

Categories
JavaScript Answers

How to wait until an element is visible with JavaScript Puppeteer?

To wait until an element is visible with JavaScript Puppeteer, we call waitForSelector.

For instance, we write

const puppeteer = require("puppeteer");

const broswer = await puppeteer.launch();
const page = await browser.newPage();
await page.waitForSelector("#myId");
browser.close();

to call launch to launch the browser.

We call awaitForSelector to wait for the element with ID myId to show.

Categories
JavaScript Answers

How to destroy cookie with Node.js?

To destroy cookie with Node.js, we call clearCookie.

For instance, we write

res.clearCookie("key");

to call clearCookie to clear the cookie with key 'key'.

Categories
JavaScript Answers

How to combine or merge JSON on Node.js?

To combine or merge JSON on Node.js, we use the spread operator.

For instance, we write

const o1 = { a: 1 };
const o2 = { b: 2 };
const obj = { ...o1, ...o2 };

to create the obj object by spreading the properties of o1 and o2 into a new object.

Categories
JavaScript Answers

How to export an async function in Node.js?

To export an async function in Node.js, we set the module.exports property.

For instance, we write

const doStuff = async () => {
  // ...
};

module.exports.doStuff = doStuff;

to define the doStuff function and set that as the value of the module.exports.doStuff to export it.