0

I have the following code that is supposed to call an api using the object key value and then open a text file and replace the text that matches the key with the API data. To simplify I've removed the API to just loop through they obj keys and then attempt to replace.

const fs = require('fs')
const storage  = require('./db/storage')


const keys =  Object.keys(storage.MOVIES);

for (const key of keys) {
    fs.readFile('./db/movies_prebuilt.txt', 'utf8', function (err,data) {
        if (err) {
          return console.log(err);
        }
        //THIS LINE DOESNT WORK using key. but does work using an actual 
        string.
        var result = data.replace(key, "NOTWORKING");
        fs.writeFile('./movies.md', result, 'utf8', function (err) {
           if (err) return console.log(err);
        });
      });
    }

The db/storage snippet looks like this:

exports.MOVIES = {
    "Matrix_Score": "tt0133093",
    "2001_Space_Score": "tt0062622"
     ...
}

And The replacement text inside looks like this

 ### Matrix_Score

And I have changed the for loop to data.replace("Matrix_Score","WORKING"); and that works perfectly fine. Is it not possible to use the key as a string? The only reason I want to use it, is so I dont have to write another array or object with all the names, when I have it already.

cheers!

6
  • 1
    The line var result = data.replace(key, "WORKING"); doesn't replace the text with what the key is. Commented Mar 1, 2019 at 0:37
  • 1
    Did you verify that keys actually contains the values you expect? Commented Mar 1, 2019 at 0:38
  • Yes, I've printed them and have all the keys when looped through. Commented Mar 1, 2019 at 0:39
  • "Is it not possible to use the key as a string?" key is a variable that holds a string value. A string value is always a string. Commented Mar 1, 2019 at 0:39
  • What is the result for the Matrix_Score key if you do console.log(key, key === "Matrix_Score");. What if you do console.log(key, key.length) ? Commented Mar 1, 2019 at 0:40

1 Answer 1

1

I think this following code may be closer to what I am understanding the intention is. fs.readFile returns all of the file contents, and you likely want to replace all of the keys, not just the last one. Also no need to read the file multiple times:

const fs = require('fs')
const storage  = require('./db/storage')

const keys =  Object.keys(storage.MOVIES);

fs.readFile('./db/movies_prebuilt.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  for (const key of keys) {
    data = data.toString().replace(key, "NOTWORKING");
  }
  fs.writeFile('./movies.md', data, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});

EDIT to add back the original point

fs.readFile typically returns a Buffer which needs to be .toString'd to be manipulated.

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

8 Comments

That doesn't explain why it would work if the are using a string literal.
yeah I mis-read and jumped to the first problem I saw. I will update with more suggestions.
That fixes the issue. I should of known better :)
Isn't the buffer returned only if no encoding is specified, and a string otherwise? But OP did specify an encoding, UTF-8. docs
Yup, I throw a data.toString().replace(key,"WORKING") and it worked exactly as expected.
|

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.