Polymer 2.0 - Can you write to fs? - javascript

I'm working on an MVP for a project, and I'm trying to mock up a "database" quick and dirty. I thought for now I'll just put my "database" into a .json file and work with that. I am able to use iron-ajax to get a file read in to a Polymer property to be manipulated, however, I don't know how I could write it back onto the filesystem once I manipulated it. I tried
let fs = require('fs');
fs.writeFile('./db/db.json', json, 'utf8');
However, this does not work (apparently, require does not work on the client side). I've tried googling around and checking the answers on the linked thread, but the answers are quite vague ("use <script> tag" - okay, but how?) and I haven't been able to figure it out. How would I be able to pass a json object and write it back to the filesystem?

Simple answer is no you can't. You have to write a little backend for that but for that i usually use the localStorage there you can store JSON and read write.

Related

Is there no simple way to read a local JSON or a TEXT file using plain Javascript?

I have a local JSON file. I want to create an object of that file using JS.
I tried finding some solutions and they are simply too complicated
An answer suggests that "Being able to read a local file from your browser would be a major breach of security..." so does that mean that there is NO simple way to read a local file without using FileReader or XMLHttpRequest !?
I am very disappointed as I come from a Python background and there, you can simply use a one liner "open" function to read a file.
(PS: Please do not use any AJAX or jQuery in your answers/comments)
EDIT: Somewhat more detailed description.
I have 4 files in a folder. An HTML, a CSS, A JS and one JSON file. What I want is whenever, I try to load the HTML page in a browser, my JS creates a JSON object of my local JSON file which I can display in my HTML body.

Call Python script from local JavaScript App

so I've looked around quite a bit now and wasn't able to find quite the use case I think I am confronted with.
For some background:
I'm fairly new to JavaScript and have never had to call any other program/script from it. Now I did develop a Python script that pulls some data from online sources, formats it and dumps it into JSON files. In order to display this data in a proper way I figured I would use Electron.
While handling the JSON files is completely fine (would be quite sad if it wasn't I guess), I need to be able to call the Python script updating the data from my Electron UI. As everything is local, I hoped, that there would be an easier way, than setting up some server for the Python script to run on, just to be able to trigger its execution from my Desktop App. This is especially true, as I don't even need to get or process any returns, I just want to trigger the execution of that script.
So the question now is: is there such an "easy" way to execute Python scripts from an Electron/JavaScript based locally saved Desktop app?
Thanks in advance for any answers!
Like a previous commenter mentioned, you should be able to follow this SO answer in Node.js (which is what Electron uses).
To expound upon that answer just a bit, I'd recommend using the built-in Python JSON utility to dump JSON to the standard out (just printing out the JSON string), and the using the built-in Node.js JSON utility to parse that JSON string into a javascript object for use in your application.
Alright, so after being redirected to this thread, which I can only recommend reading through if you have an interest in this issue, I took their solution and altered a little, which took me a bit of time, due to some confusion, which I now would like to spare you guys!
To re-introduce the issue: The goal is to call a python script from a JavaScipt/Electron based UI. The python script only needs to be executed, but it needs to happen onClick, as it is an update function.
Now this is the code I used:
const exec = require("child_process").exec;
function triggerUpdateAndRefreshFooter() {
exec('python relativePathToScript/update.py',
function(error, stdout, stderr) { //callback function, receives script output
refreshFooter(); //don't use the output, but I could here
}
)
}
I did have some issues figuring out all of that const stuff in the other thread, as well as having to guess IF I could just execute my script in a separate function. In the end this did work!
I hope this was helpful!

Pushing to an array in a separate JSON file

Lets say I have a JSON file, with an array inside of that JSON file.
{
"users": ["288381238123", "12312123123"]
}
Now I want to push to that array, and I have that file required at the top
const userList = require('../rolecall.json');
Then I have some other code here, which is seemingly supposed to push to that array
const users = userList.users;
users.push('82313123');
Now when I check back to the JSON file, there is no addition to the array. Why is that? If anyone could help me, that would be great.
When you 'push' to the array in Javascript, you are only changing the Javascript object.
Edit: taking Lyon's comment into account
The short answer is, you can't change a file on the client machine using javascript. That would be a huge security issue (though it used to be possible, fun times).
However, you can ask the browser to display a save dialog asking the user whether they want to save the content of your json to a file.
Old answer
If you want to save the changes to a file located on the server, you need to send the informations to said server, preferably via a POST request.
A simple way to do it is to use Ajax requests.
You can use the JSON.stringify method to format your users object.
xhttp.send(JSON.stringify(users));
Then you can handle the request on the server. The way to do that depends on the language used (PHP, Ruby, Node, etc)
Observations :
You are pushing a element into a local array userList. If you want to update the rolecall.json file, you'll need to write back to that file.
Javascript (client side) has not functionality to create, edit etc.. files.You can do it in back-end side.
Workaround :
You got your json in userList constant variable. now you can push that element into the array and then write back to rolecall.json if you're manipulating it in your backend (ie.: NodeJs)
const userList = {
"users": ["288381238123", "12312123123"]
};
userList.users.push('82313123');
console.log(userList);
In node.js you can use the fs library.
fs = require('fs');
fs.writeFile('fileName.json', JSON.stringify(userList));

Load a JSON file from a URL without AJAX request?

So I can't seem to find an straight answer to my problem.
What I want to do is load a JSON file that's located on a specified URL. It's a large complicated JSON file. Basically, I've been trying to use AJAX to 'get' the file all night long. I've used the $.getJSON Jquery shortcut too, without any luck. I believe the server isn't allowing for AJAX requests, but if I enter in the URL I can access the file without a problem.
My question is, is there any way I can get the file so I could use it in order to create this little mock up site that I need to create?
Is this possible?
I've tried looking for a way to just rip the text off of the URL and then just turning that into a JSON file so I could parse it, but I haven't found any relevant information on this tactic.
If you have any ideas, please let me know, thank you! :D

How to use the Glimpse client viewer but NOT the server implementation

I would like to use the Glimpse client viewer in a web application in order to render some JSON. I am unable to use the Glimpse server implementation on the site in question. However, I can implement my own IHttpHandler to render the information using the Glimpse JSON format.
Has anyone done this and posted details on how to do it? If not, can anyone tell me the steps required to get this up and running? Alternatively, are there any other similar viewer frameworks out there?
Note: I am poking around the source and have seen the client js etc. I will continue down the source hacking route, but was hoping someone may have some shortcuts for me!
As Nik said I would be interested in what you are trying to do. But in the mean time the best place to look is http://getglimpse.com/Protocol.
If you look on this page you will see that we have built a protocol tester. This allows you to put in any JSON and see the output.
If you want to do this yourself, have a look at http://getglimpse.com/Scripts/Protocol/LayoutExample.js and you will see how we do this without using the whole of Glimpse.
You will see that we are doing something like the following:
var data = { test : 'test', hello : 'hello' };
var html = $Glimpse.glimpseProcessor.build(data, 0, false)
$('.panel').html(html);
I know this isn't as nice as it could be but it wasn't designed with this in mind.
We are currently working on refactoring the client code to make this all better.
We haven't really documented all of this yet.
Your best bet is to look at first glimpse javascript file that gets rendered to a page - it is the data file. If you can output data in that format, which is basically just one object of key value pairs, then the client will pick up the data and render it.
You might also want to look at the Glimpse.PHP implementation, since they'vve had to do the same thing you are.

Categories