Categories
JavaScript Answers

How to fix react-native run-android is unrecognized error with Node?

To fix react-native run-android is unrecognized error with Node, we should run this in the React Native project folder.

We run cd to change to the React Native project folder before running react-native run-android.

Categories
JavaScript Answers

How to fix Sass Loader Error: Invalid options object that does not match the API schema with JavaScript?

To fix Sass Loader Error: Invalid options object that does not match the API schema with JavaScript, we make sure the Webpack config has properties in the right places.

For instance, we write

module.exports = {
  loader: "sass-loader",
  options: {
    sassOptions: {
      indentedSyntax: true,
    },
  },
};

in webpack.config.js, to add the indentedSyntax property into sassOptions.

Categories
JavaScript Answers

How to download file stream and writeFile with Node.js axios?

To download file stream and writeFile with Node.js axios, we set the responseType.

For instance, we write

const response = await axios({
  method: "get",
  url: "https://example.com/my.pdf",
  responseType: "stream",
});
response.data.pipe(fs.createWriteStream("/temp/my.pdf"));

to call axios to make a get request.

We set the responseType to 'stream' to download the data as a stream.

And then we call response.data.file with a write stream to pipe the read stream we get from response to the write stream to write the response data to the my.pdf file.

Categories
JavaScript Answers

How to parse XLSX with Node and create JSON?

To parse XLSX with Node and create JSON, we use the xlsx module.

For instance, we write

const XLSX = require("xlsx");
const workbook = XLSX.readFile("master.xlsx");
const sheetNameLlist = workbook.SheetNames;
console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheetNameLlist[0]]));

to call readFile to read master.xlsx.

And we get the sheets names from SheetNames.

Next, we call sheet_to_json with the sheet we get from workbook.Sheets[sheetNameLlist[0]] to return the sheet data as JSON.

Categories
JavaScript Answers

How to load file with Node?

To load file with Node, we call the readFile method.

For instance, we write

const fs = require("fs");

fs.readFile(__dirname + "/test.txt", (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data.toString());
});

to call readFile with path of the file to read from.

And we get the file data from data in the callback.