• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Read local txt file from node_helper.js

Scheduled Pinned Locked Moved Troubleshooting
12 Posts 2 Posters 2.4k Views 2 Watching
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 Offline
    Aphamguin
    last edited by Aug 24, 2020, 8:52 PM

    I am currently creating my own MM module, but am still relatively new to the platform so I used this tutorial to help me get started: https://forum.magicmirror.builders/topic/8534/head-first-developing-mm-module-for-extreme-beginners
    It mentions that the node_helper.js is able to read from local files, but I’m having trouble getting it to work.

    Here’s what my node_helper.js looks like so far:

    var NodeHelper = require("node_helper")
    var fs = require('fs')
    
    module.exports = NodeHelper.create({
    	start: function() {
    		this.rawText = fs.readFileSync('YourTrueHome.txt').toString()
    		this.readIn = this.rawText.split('\r')  //array of book line by line
    		this.text = this.readIn[1]
    	},
    socketNotificationReceived: function(notification, payload) {
    	switch(notification) {
    		case "DO_YOUR_JOB":
    			this.sendSocketNotification("I_DID", this.text)
    			break
    		}
    	},
    })
    

    And then in the main js file, the socket notification receiver simply reads:

    socketNotificationReceived: function(notification, payload) {
    		switch(notification){
    			case "I_DID":
    				let elem = document.getElementById("COUNT")
    				elem.innerHTML = "Count: " + payload
    				break
    		}
    	},
    

    There is a COUNT element correctly defined in getDom: function() already.

    S 1 Reply Last reply Aug 24, 2020, 9:13 PM Reply Quote 0
    • S Offline
      sdetweil @Aphamguin
      last edited by Aug 24, 2020, 9:13 PM

      @Aphamguin what is the problem? I don’t see anything obvious

      also, see my Sample Module, which sends thru node_helper and gets response back which is shown.

      https://github.com/sdetweil/SampleModule

      if u are just showing text, you should use innerText, so the dom handler doesn’t have to examine your text only to find no html tags… (save cycles for other work)

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      A 1 Reply Last reply Aug 24, 2020, 9:48 PM Reply Quote 0
      • A Offline
        Aphamguin @sdetweil
        last edited by Aug 24, 2020, 9:48 PM

        @sdetweil it seems to break the module and MM. Nothing shows up on the screen when I run it.
        Thanks for the sample module. It’s a really helpful template that I thought MM would have given us.
        It’s a simple module, but I’m just implementing its functionality in phases. At the moment I just want it to read from a local txt file, parse it into an array, and then display a chunk of text by accessing that array. So yes I’m just showing text. What is innerText? Do I just do elem.innerText(“text”)
        instead of elem.innerHTML(“text”)?

        S 3 Replies Last reply Aug 24, 2020, 10:05 PM Reply Quote 0
        • S Offline
          sdetweil @Aphamguin
          last edited by sdetweil Aug 24, 2020, 11:34 PM Aug 24, 2020, 10:05 PM

          @Aphamguin yes elem.innerText.

          not sure what you mean by breaks mm…

          if u email me the three files I will find what the problem is.

          same user ID on gmail. rename the .js as .txt

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @Aphamguin
            last edited by Aug 24, 2020, 10:09 PM

            @Aphamguin you did set a position for your module in config, right? else the output doesn’t get used

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • S Offline
              sdetweil @Aphamguin
              last edited by Aug 24, 2020, 11:35 PM

              @Aphamguin the sample mm provides (hello world), uses the nunjuks template approach

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              A 1 Reply Last reply Aug 25, 2020, 12:26 AM Reply Quote 0
              • A Offline
                Aphamguin @sdetweil
                last edited by Aug 25, 2020, 12:26 AM

                @sdetweil I have emailed you the main js file, the helper, and the txt file, all as txt files. I hope you got it.
                I did set a position, yes.
                Where can I get the sample mm? What is the nunjuks template approach?

                S 1 Reply Last reply Aug 25, 2020, 2:20 AM Reply Quote 0
                • S Offline
                  sdetweil @Aphamguin
                  last edited by Aug 25, 2020, 2:20 AM

                  @Aphamguin my sample module
                  https://github.com/sdetweil/SampleModule

                  I don’t have any links to nunjuks.

                  but basically you create a layout template with locations for variables, and then supply the data, and the template engine does that formatting
                  see the helloworld module in the defaults folder

                  I am working on your module. the initial problem is the data filename has to have a path, else it will be in the MagicMirror folder.

                  but there is also an easier way to process your data.

                  in a couple hours I will get it back to you the same way

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  A 1 Reply Last reply Aug 25, 2020, 2:48 AM Reply Quote 0
                  • A Offline
                    Aphamguin @sdetweil
                    last edited by Aug 25, 2020, 2:48 AM

                    @sdetweil Ok I’ll play around with it then.
                    Wow great. Thanks a lot. I really appreciate it.

                    S 1 Reply Last reply Aug 25, 2020, 3:47 AM Reply Quote 0
                    • S Offline
                      sdetweil @Aphamguin
                      last edited by Aug 25, 2020, 3:47 AM

                      @Aphamguin sent back

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/12
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy