timdows / MMM-JsonTable

A module for the MagicMirror project which creates a table filled with a list gathered from a json request.
MIT License
25 stars 31 forks source link

How to load local JSON file #11

Closed 4r3st3r closed 4 years ago

4r3st3r commented 4 years ago

Hi, Is there any way to load a local JSON file?

I am currently running a python3 script to create a .json file in the MMM-JsonTable directory in valid JSON, but it does not seem to load the file, it just says "Awaiting JSON"? I have tried loading a file from an online URL and it loaded fine? Is it just not supported to load a local file?

Cheers in advance!

timdows commented 4 years ago

That is a good question, getting the node_helper.js is just executing a request to the specified URL. Can you see any debugging errors in the browser console?

If there is nothing there, add console.log(error, response, body); between line 17 and 18 of https://github.com/timdows/MMM-JsonTable/blob/master/node_helper.js

4r3st3r commented 4 years ago

WOW! Hi Tim! First off, thanks so much for this module! Its so helpful!!! Second thanks for helping me sort out my problem!!! You're the best!! When I run the module with a local JSON file as the URL specified in the Mirror's config.js the log reads this:

0|MagicMirror | [2020-05-15 18:44:59.648] [LOG] 0|MagicMirror | Error: Invalid URI "file:///home/pi/MagicMirror/modules/MMM-JsonTable/output.json" 0|MagicMirror | at Request.init (/home/pi/MagicMirror/node_modules/request/request.js:273:31) 0|MagicMirror | at new Request (/home/pi/MagicMirror/node_modules/request/request.js:127:8) 0|MagicMirror | at request (/home/pi/MagicMirror/node_modules/request/index.js:53:10) 0|MagicMirror | at Class.getJson (/home/pi/MagicMirror/modules/MMM-JsonTable/node_helper.js:12:3) 0|MagicMirror | at Class.socketNotificationReceived (/home/pi/MagicMirror/modules/MMM-JsonTable/node_helper.js:26:9) 0|MagicMirror | at Socket.<anonymous> (/home/pi/MagicMirror/js/node_helper.js:113:11) 0|MagicMirror | at Socket.emit (events.js:200:13) 0|MagicMirror | at /home/pi/MagicMirror/node_modules/socket.io/lib/socket.js:528:12 0|MagicMirror | at processTicksAndRejections (internal/process/task_queues.js:82:9) undefined undefined

Does that make sense? Am I missing something obvious? Thanks again for your help!!! P.S. sorry for bad formatting , I cant seem to get it to show it line by line?

timdows commented 4 years ago

I guess that request.js cannot serve local files if I check the code https://github.com/request/request/blob/master/request.js

Solution (not tested) could be:

getJson: function (url) {
    var json = require('file:///home/pi/MagicMirror/modules/MMM-JsonTable/output.json');
    self.sendSocketNotification("MMM-JsonTable_JSON_RESULT", {url: url, data: json});
},

And for that to work you'd have to install require https://requirejs.org/ npm install requirejs?