Categories
JavaScript Answers

How to remove object from array with MongoDB?

Sometimes, we want to remove object from array with MongoDB.

In this article, we’ll look at how to remove object from array with MongoDB.

How to remove object from array with MongoDB?

To remove object from array with MongoDB, we can use the $pull operator.

For instance, we write

db.mycollection.update({
    '_id': ObjectId("5150a1199fac0e6910000002")
  }, {
    $pull: {
      items: {
        id: 23
      }
    }
  },
  false,
  true,
);

to call update with an object that has the $pull property with items.id set to 23 to remove the items entry with id 23.

We pass in false to disable upsert and pass in true to let us update multiple entries.

Conclusion

To remove object from array with MongoDB, we can use the $pull operator.

Categories
JavaScript Answers

How to respond with a JSON object in Node.js and Express?

Sometimes, we want to respond with a JSON object in Node.js and Express.

In this article, we’ll look at how to respond with a JSON object in Node.js and Express.

How to respond with a JSON object in Node.js and Express?

To respond with a JSON object in Node.js and Express, we can call the res.json method.

For instance, we add

res.json({
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
});

in our route handler function to respond with

{
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
}

res.json takes any plain JavaScript object and return it as a JSON string response.

Conclusion

To respond with a JSON object in Node.js and Express, we can call the res.json method.

Categories
JavaScript Answers

How to create blobs with Node.js?

Sometimes, we want to create blobs with Node.js.

In this article, we’ll look at how to create blobs with Node.js.

How to create blobs with Node.js?

To create blobs with Node.js, we can use the Buffer.from method.

For instance, we write

const buffer = Buffer.from(arrBuffer);
const arrayBuffer = Uint8Array.from(buffer).buffer;

to call Buffer.from with the arrBuffer array buffer object to create a buffer.

Then we call Uint8Array.from with a buffer to convert the buffer to an Uint8Array and get the array buffer with the buffer property.

Conclusion

To create blobs with Node.js, we can use the Buffer.from method.

Categories
JavaScript Answers

How to add file upload with Node.js and Express?

Sometimes, we want to add file upload with Node.js and Express.

In this article, we’ll look at how to add file upload with Node.js and Express.

How to add file upload with Node.js and Express?

To add file upload with Node.js and Express, we can use the express-fileupload package.

To install it, we run

npm i express-fileupload

Then we use it by writing

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

app.use(fileUpload());

app.post('/upload', (req, res) => {
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.');
  }

  const {
    sampleFile
  } = req.files;

  sampleFile.mv('/somewhere/on/your/server/filename.jpg', (err) => {
    if (err) {
      return res.status(500).send(err);
    }
    res.send('File uploaded!');
  });
});

to call app.use with the middleware we get from calling fileUpload.

Then we check if req.files is defined and has any entries in it in the POST /upload route handler.

If req.files isn’t defined or has no length, then we return a 400 response.

Otherwise, we get the file and call mv to save the file as '/somewhere/on/your/server/filename.jpg'.

The callback we pass into mv when mv is done.

Conclusion

To add file upload with Node.js and Express, we can use the express-fileupload package.

Categories
JavaScript Answers

How to call Node.js fs.writeFile as a promise?

Sometimes, we want to call Node.js s.writeFile as a promise.

In this article, we’ll look at how to call Node.js fs.writeFile as a promise.

How to call Node.js fs.writeFile as a promise?

To call Node.js fs.writeFile as a promise, we can use the fs‘s promises module.

For instance, we write

const fs = require('fs').promises;

(async () => {
  const file = await fs.readFile('filename.txt', 'utf8');
  await fs.writeFile('filename.txt', 'test');
})()

to require the promises version of fs with

const fs = require('fs').promises;

Then we call fs.readFile with path of the file to read and the string with the encoding of the file.

And then we call fs.writeFile to write to filename.txt file and 'test' as its content.

Conclusion

To call Node.js fs.writeFile as a promise, we can use the fs‘s promises module.