Navigation

    MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Read local txt file from node_helper.js

    Troubleshooting
    2
    12
    52
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Aphamguin last edited by

      To conclude this issue, when reading from a local txt file with node_helper.js, you have to make sure the file name has a path. And then when parsing it, you can parse it easily by checking for carriage returns (\r) to see if if its a windows file. Here is a snippet of the solution:

      this.rawText = fs.readFileSync(__dirname + '/localTxtFile.txt').toString()
      this.isWindows = (this.rawText.indexOf('\r') == -1) ? false:true
      this.variable= this.rawText.split(this.isWindows ? '\r\n\r\n' : '\n\n')
      
      S 1 Reply Last reply Reply Quote 0
      • S
        sdetweil @Aphamguin last edited by sdetweil

        @Aphamguin well, you think about what you are trying to accomplish,

        I changed the file to Linux line ends. then realized u might get it from somewhere as windows line end. so added to make it work either way.

        1 Reply Last reply Reply Quote 0
        • 1
        • 2
        • 2 / 2
        • First post
          Last post
        Enjoying MagicMirror? Please consider a donation!
        MagicMirror created by Michael Teeuw.
        Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy