Categories
JavaScript Answers

How to fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’?

To fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’, we should set the build target to a recent version of JavaScript.

For instance, we write

{
  //...
  "compilerOptions": {
    "target": "ES2017"
  }
}

in tsconfig.json to set the TypeScript compiler build target to ES2017 to avoid transpilation of ES6 classes to ES5 constructor functions that cause this error.

Categories
JavaScript Answers

How to time out a Node Promise if failed to complete in time?

To time out a Node Promise if failed to complete in time, we use the Promise race method.

For instance, we write

const race = Promise.race([
  new Promise((resolve) => {
    setTimeout(() => {
      resolve("I did it");
    }, 1000);
  }),
  new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("Timed out");
    }, 800);
  }),
]);

try {
  const data = await race;
} catch (e) {
  console.log(e);
}

to call Promise.race with an array of promises.

We call reject in the 2nd Promise callback to reject the promise after 800ms.

Then we use await to run the promise returned by race.

And then we get the data from the promise.

We catch the rejection with the catch block.

Categories
JavaScript Answers

How to fix TypeError: db.collection is not a function with Node MongoDB?

To fix TypeError: db.collection is not a function with Node MongoDB, we should make sure we’re getting the collection from the database object.

For instance, we write

MongoClient.connect(db.url, (err, database) => {
  const myAwesomeDB = database.db("myDatabaseNameAsAString");
  myAwesomeDB.collection("theCollectionIwantToAccess");
});

to call connect to connect to the database.

And we get the database from the database.db method.

We then get the collection from the database’s collection property.

Categories
JavaScript Answers

How to check that two objects have the same set of property names with JavaScript?

To check that two objects have the same set of property names with JavaScript, we use the Object.keys method.

For instance, we write

const compareKeys = (a, b) => {
  const aKeys = Object.keys(a).sort();
  const bKeys = Object.keys(b).sort();
  return JSON.stringify(aKeys) === JSON.stringify(bKeys);
};

to define the compareKeys function.

In it, we get the keys of objects a and b with Object.keys in an array.

Then we sort both arrays with sort.

Then we convert both arrays to JSON strings with JSON.stringify and compare to see if they’re equal.

Categories
JavaScript Answers

How to use webpack with Node Express?

To use webpack with Node Express, we create a config that builds with 'node' as its target.

For instance, in webpack.config.js, we write

module.exports = [
  {
    name: "server",
    entry: "./src/server/index.js",
    target: "node",
    output: {
      path: __dirname + "/dist/server",
      filename: "bundle.js",
    },
  },
  {
    name: "client",
    entry: "./src/client/index.js",
    // target: 'web', // by default
    output: {
      path: __dirname + "/dist/client",
      filename: "bundle.js",
    },
  },
];

to set the target property to 'node' in the first object to set the build target of the server app to Node.

We set the entry property to the entry point file path.

And we set the output property to an object with the bundle file path.