0

Im kind of new to JavaScript and I wanted to open a text file, read from it to an object, then close it.

I made an object like this:

const users = {
    data: new Array()
} 

And after this I've tried, to push the thisUser object to the data list of the users, but it won't do it.

openFile() {
    const fs = require('fs')
    fs.readFile("some.txt", "utf-8", function(err, data) {
    
        if (err) { 
            throw err;
        }

        data = data.toString().split("\r\n")
        for (var i = 0; i < data.length; i++) {
            
            const line = data[i].split(";");
            var thisUser = {
                email:line[0],
                passw:line[1]
            }
            users.data.push(thisUser);
            console.log(line[0], line[1]);
        }
    })
}

openFile()

console.log(users);

I'm really stuck here and I couldn't find a solution for this.

I've tried to push it simply to an Array, but it doesn't work either. How could I do it, or what's seems to be the problem here?

krusoadam
  • 13
  • 3
  • What "doesn't work" about this? What is the specific error or unexpected result that you're observing? – David Jun 15 '23 at 19:01
  • Is this in a web page (because your code only works on a server or called from the command line)? What is in the file exactly? lines of emails and passwords delimited by semicolon, one set per line? How do you call openFile? Som many questions before we can help you – mplungjan Jun 15 '23 at 19:05
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jun 15 '23 at 19:06
  • There are lines of email and password combinations (not real ones), and it listed like this: some@email.com;password1 another@email.com;password2 It doesn't give me an error, but i can't see any changes in the users object. – krusoadam Jun 15 '23 at 19:08
  • @krusoadam: *"but i can't see any changes in the users object"* - Where/how are you observing changes? The code shown never does anything with that object. Immediately after the closing curly brace of the `for` loop (or even immediately after the `.push()` operation), if you log `users` to the console, what gets logged? – David Jun 15 '23 at 19:12
  • After the listed codes I've tried console.log(users) and console.log(users.data) but it didn't work either. If i log it, it prints out the correct values – krusoadam Jun 15 '23 at 19:14
  • Do you actually CALL openFile() anywhere? – mplungjan Jun 15 '23 at 19:14
  • try `let data = fs.readFileSync(FILENAME, 'utf8');` – Ronnie Royston Jun 15 '23 at 19:15
  • 1
    I updated the code and here you will have a working example on **StackBlitz**: run in the terminal: > node index.js The code will show a { data: [ { email: 'test@test.com', passw: 'test123' } ] } [Working example][1]. [1]: https://stackblitz.com/edit/node-kngrhl?file=index.js – Filip Huhta Jun 15 '23 at 19:16
  • 1
    @mplungjan: There's no code in the callback observing the value of `users` though. And the OP [indicated](https://stackoverflow.com/questions/76484747/cant-add-to-an-object-when-opening-a-file?noredirect=1#comment134859445_76484747) that they are observing it "after the listed codes", which I'm taking to mean as after all of the code shown. (So outside of the asynchronous operation.) If the OP can provide a [mcve] demonstrating otherwise, that would be great. But so far all we really have are assumptions and guesses, and this seems *pretty likely*, especially given the scope for `users`. – David Jun 15 '23 at 19:18
  • 1
    Here is my version. https://stackblitz.com/edit/node-uvyv9h?file=some.txt,index.js type `node index.js` in the terminal window – mplungjan Jun 15 '23 at 19:23
  • 1
    @David Right. OP should add the console.log here: ` users.data.push(thisUser) } console.log(users); })` – mplungjan Jun 15 '23 at 19:25
  • I've updated the question with my logs, and I've tried to put console.log(users), after the users.data.push(), in the for loop as you suggested and in that scope it works. Thank you for your help and suggestion! I've just started studying JS HTML and CSS so these things are really new to me. – krusoadam Jun 15 '23 at 20:54

0 Answers0