1

I have 3 files:

Ingredients.js

const fs = require("fs");
const readline = require('readline');
const stream = require('stream');


const ingredients = () => {
    const instream = fs.createReadStream('ingredients.txt');
    const outstream = new stream;
    const rl = readline.createInterface(instream, outstream);

    const listIngredients = {};


    rl.on('line', function (line) {
        let lower = line.toLowerCase();
        listIngredients[lower] = 0; 
    });

    rl.on('close', function () {
        console.log('listIngredients', listIngredients);
    });
}

module.exports = ingredients;

cookbook.js:

let fs = require("fs");

const book = () => {

    const regex = /\b(\w+)\b/g;

    fs.readFile('cook-book.txt', 'utf8', function (err, data) {

        let book = data;
        let lower = book.toLowerCase();
        let split = lower.match(regex);
        console.log(split);
    });

}
module.exports = book;

compare.js

const ingredients = require('./ingredients');
const book = require('./book');

I'm trying to increase the key values of ingredients every time they are mentioned in the cookbook. I think this should go into a different js file to make it cleaner.

Whilst i can console.log out the information from the above files, I cannot figure out how to actually access the data and make changes to the ingredients object in compare.js?

3
  • 1
    ingredients isn't an object, it's a function Commented Apr 15, 2020 at 10:28
  • Both files are read async, and the functions you're requiring in compare.js don't return anything. So with your current code, you cannot access either. You have to either switch to Sync versions of readFile, or use Promises / async+await. Commented Apr 15, 2020 at 10:29
  • Is switching to readFileSynce as easy as this: let data = fs.readFileSync('oliver-twist.txt', "utf-8"); const regex = /\b(\w+)\b/g; let lower = data.toLowerCase(); let split = lower.match(regex); console.log(split); Commented Apr 15, 2020 at 12:31

1 Answer 1

1

as others noticed your ingredients and book variables are functions having required information inside their scope and not returning it outside. to fix it, you have to return values.

as you're working with asynchronous stuff, your functions should be wrapped into Promise's to handle the flow correctly.

this code should help you:

const fs = require('fs');
const readline = require('readline');
const { Writable } = require('stream');
const fsp = fs.promises;

// ingredients.js
const getIngredients = async () => new Promise((resolve, reject) => {
  const instream = fs.createReadStream('ingredients.txt');
  const outstream = new Writable();
  const rl = readline.createInterface(instream, outstream);

  const listIngredients = {};

  rl.on('line', line => {
    const lower = line.toLowerCase();
    listIngredients[lower] = 0;
  });
  rl.on('error', reject);
  rl.on('close', () => resolve(listIngredients));
});

// cookbook.js
const getBookContent = async () => new Promise(async (resolve, reject) => {
  try {
    const wordRegEx = /\b(\w+)\b/g;
    const book = await fsp.readFile('cook-book.txt', 'utf8')
    const lower = book.toLowerCase();
    return resolve(lower.match(wordRegEx));
  } catch (error) {
    return reject(error);
  }
});

// compare.js
(async () => {
  const ingredients = await getIngredients();
  const words = await getBookContent();
  console.log(ingredients);
  console.log(words);
})();

the names of functions have been change for better representations of their instances.

i've also used an async iife to use async/await syntax, however you can still work with Promises themselves

Sign up to request clarification or add additional context in comments.

4 Comments

Can you please explain how I import the ingredients and cookbook files into a seperate compare.js? I'm trying to use a module export but cannot figure it out?
try exports.<name> = <something> to export your functions or module.exports = { <name>: <something> } if you want it this way. read more at nodejs.org/api/modules.html#modules_modules
Its calling it within the compare.js file that I'm struggling with, i've got exports.<name> = <something> on both files. Importing it within compare.js is where don't know the correct way of doing it so when i node compare.js it runs?
in compare.js use const { getBookContent } = reqiure('./cookbook.js') while in cookbook.js you should have exports.getBookContent = getBookContent; and getBookContent is defined previously

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.