Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

node.js - How to preserve text format when return in nodejs/express?

Here is a text file in console output on nodejs/express backend server and it carries neat format:

 abi: [
        {
                "inputs": [
                        {
                                "internalType": "uint256",
                                "name": "_value",
                                "type": "uint256"
                        },
                        {
                                "internalType": "string",
                                "name": "_itemName",
                                "type": "string"
                        },
...
]

When the file above is received in mobile front end (React Native), it becomes:

 abi: '[
{
"inputs": [
{
"internalType": "uint256",
"name": "_value",
"type": "uint256"
},
{
"internalType": "string",
"name": "_itemName",
"type":...]'

The format and content of the text file has been changed by addition of and and it can't be used as input for a function call. Is there way the original nice and neat format can be preserved or restored when returned from a nodejs server?

UPDATE:

The text string is returned on nodejs/express server as json format:

return res.status(200).send({dude:_dude.deploy_address, fex:_fex.deploy_address, forsale:{abi:_forsale.abi, bytecode:_forsale.bytecode}});

Here is the fetch from the mobile:

let res = await fetch(beUrl, {method: "GET", headers: {
                                                'Accept': 'application/json, text/plain',
                                                'Content-Type': 'application/json',   
                                              }});
let res1 = await res.json();
question from:https://stackoverflow.com/questions/65840853/how-to-preserve-text-format-when-return-in-nodejs-express

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is how the format was reserved and restored:

On nodejs/express server, JSON.stringift the whole return object:

let obj = JSON.stringify({dude:_dude.deploy_address, fex:_fex.deploy_address, forsale:{abi:(_forsale.abi), bytecode:(_forsale.bytecode)}});
    return res.status(200).send(obj);

On front end, JSON.parse the abi and bytecode:

let res = await fetch(beUrl, {method: "GET", headers: {
                                                'Accept': 'application/json, text/plain',
                                                'Content-Type': 'application/json',   
                                              }});
      let res1 = await res.json();
      let res3 = JSON.parse(res1.forsale.abi);
      let res4 = JSON.parse(res1.forsale.bytecode);

res3 and res4 have the text format restored to its original as it is on the nodejs/express server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...