Best way to write to JSON file in Node 10/Angular 7? - json

I'm not sure if this question belongs here, but this is driving me insane.
I am running Node v10.16.0. Sorry!
Since in Angular 7 fs no longer works - what is the best way to write to a JSON file?
Importing a JSON file now works nice and easy, but how do I write to it?
Edit: Adding what I tried.
component.ts:
import * as fs from 'fs';
[...]
saveChanges(changes) {
fs.writeFile('config.json', JSON.stringify(config), null);
Error: `Module not found: Error: Can't resolve 'fs' `

Calling fs from within Angular is not possible - and usually not what you want to do.
Angular is a Front-End Framework that is used to bind data to a UI. This happens in a Browser environment, not NodeJS.
What you are most likely doing is serving your Angular application using some kind of NodeJS Server (like express, or the Angular CLI).

Related

Flask TypeError: Object of type Decimal is not JSON serializable

We are using flask for our application service. Internally we are using Jsonify to return the response and our response contains decimal data.
Couple of things to mention here.
We are using Windows machine
Some of my colleagues they are not getting any issue with the data with same configuration.
One thing we observed is flask by default uses simplejson if installed. If not, then it will
fall back to json package.
Sample data : jsonify({'response':Decimal('12345.00000')})
Our question is why for some people flask by default using simplejson and for others it's not able to identify simplejson(even though installed) and using only json throwing decimal json error.
Any idea what might be the root cause and what change needs to be done, let flask know to use simplejson over json ?
For time being we did below manual change in flask package.
Instead of jsonify({'response': Decimal('12345.00000')}) can't you just use jsonify({'response': float('12345.00000')})?
is there any chance you're using different versions of flask?
https://github.com/pallets/flask/issues/3555
in version 2, support was dropped for simplejson, but as noted in the comments, you can make it use simplejson for the JSONEncoder:
from simplejson import JSONEncoder
app.json_encoder = JSONEncoder
it also looks as though there is a fix for the decimal issue that was PR'ed but is not yet released, and will be released in version 2.0.2

how to write json dynamic to local json in angular

i have tried using fs module but I couldn't use this module in my project
const fs = require('fs');
import * as fs from ('fs')
None of the above commands don't work
You can use file saver or the solution here. You can't use node.js modules on the SPA.
If you want to do that, you need to create an API and save on the server side.
You can in cases use node modules as described in this SO question, but the browser will not allow to access the file system as you intend to do with th fs module.
fs module is node module and You can't use it on the client-side.
I suggest you use this
Save JSON string

node.js binary-tree vizualize

I encountered the following problem: I store a binary tree in the mysql table
binary-tree table
It is necessary to visualize this binary tree on WEB.
Perhaps someone has encountered a similar problem and can suggest how best to accomplish this task. In advance thanks for the answer!
After searching the libraries I came across d3. Has written an example, and like all works.
Here is my fiddle: https://jsfiddle.net/yurayazupol/vbucyuzk/1/
But then I decided to take the data from a separate json file. But after that everything stopped working. The working version is located at the link below, the interaction with the json file is in the comments in the script.js file. Can you please tell me what is wrong? https://github.com/yurayazupol/binary-tree-d3
You can't load json like this (using fs) in the browser. You can use the fetch API to load the JSON (or jQuery, axios, etc). Here's an example of using fetch:
fetch('tree.json')$
.then(response => response.json())$
.then(data => {$
root = data;$
});$
You'll need to rewrite some of your other JavaScript as the code you have written so far is synchronous and loading the json is asynchronous.

Server side data fetching

I'm trying to set up a basic component that is rendered on the server and the client. The component is populated with data from a JSON API. I've got this working on the client-side by loading my data in componentDidMount and calling this.setState when it has loaded.
The problem I have is that the data isn't loaded on the server. How do I get the initial data into the server-rendered version of my component?
https://github.com/rackt/react-router/blob/latest/docs/guides/advanced/ServerRendering.md is very vague about this:
For data loading, you can use the renderProps argument to build
whatever convention you want--like adding static load methods to your
route components, or putting data loading functions on the
routes--it's up to you.
Do you have an example anywhere of how to do this? It seems like a very basic thing for a universal application to want to do!
Yup - we have https://github.com/rackt/async-props that provides a library for dealing with async data between server and client, and provides an example for doing so. If you're using a Flux framework, you'll want to make appropriate adjustments, but the basic approach will be quite similar.

Json parsing in IBM Worklight

I am trying to achieve something like which is mentioned in this link
but i don't know whereto write the parsing code. I tried to write it in new method and added the method in "my adapter.xml" but nothing happens. I even don't know how to log in IBM WorkLight. I used WL.logger(some) but its throwing error that "Logger can not be called on an object".
Any help would be appreciated. Thanks in advance.!
In most cases you don't need to manually parse responses because WL adapter framework will do this for you. Anything you retrieve via WL.Server.invokeHttp API will be parsed to JSON automatically unless you specify returnedContentType:"plain". In case you DO need to manually parse JSON you can use JSON.parse() and JSON.stringify() APIs.
Server side logging is achieved via WL.Logger.debug/error/info etc. You can get more info about it here
You don't have to parse JSON data, there are libraries in Javascript to do that. Try JSON.parse(). You should learning how to write adapters and invoking them from clients. The Getting Started modules are a good place to start, specifically Module 4 in your case.