To convert Node.js MongoDB object id to string, we call the toString method.
For instance, we write
console.log(user._id.toString());
to call toString to convert the _id object ID to a string.
Web developer specializing in React, Vue, and front end development.
To convert Node.js MongoDB object id to string, we call the toString method.
For instance, we write
console.log(user._id.toString());
to call toString to convert the _id object ID to a string.
To fix error when running brew install node on Mac, we set a few folders’ owners to the current user.
To do this, we run
sudo chown -R `whoami`:admin /usr/local/include/node
sudo chown -R `whoami`:admin /usr/local/bin
sudo chown -R `whoami`:admin /usr/local/share
sudo chown -R `whoami`:admin /usr/local/lib/dtrace
to set the owner of the /usr/local/include/node, /usr/local/bin, /usr/local/share, and /usr/local/lib/dtrace folders to the current user.
Then we overwrite node with
brew link --overwrite node
To fix Node.js request CERT_HAS_EXPIRED error, we set the rejectUnauthorized property to false.
For instance, we write
const request = require("request");
const agentOptions = {
host: "www.example.com",
port: "443",
path: "/",
rejectUnauthorized: false,
};
const agent = new https.Agent(agentOptions);
request(
{
url: "https://www.example.com/api/endpoint",
method: "GET",
agent,
},
(err, resp, body) => {
// ...
}
);
to call request with the agent object we by calling Agent with the agentOptions to make the request.
We set rejectUnauthorized to skip checking for invalid certificate.
To write formatted JSON in Node.js, we call JSON.stringify.
For instance, we write
const formatted = JSON.stringify(object, null, 4);
to call JSON.stringify to return a JSON stringify with the object object converted to a JSON string.
We call it with 4 as the 3rd argument to indent each level with 4 spaces.
To set npm credentials using npm login without reading from stdin with JavaScript, we run npm set.
For instance, we run
npm set //<registry>/:_authToken $TOKEN
to run npm set to set the authToken to the $TOKEN environment variable value.