'Parsing a string into a format to be able to get data from JSON structure
So i was struggling to get my code to recognise this string sent from the front end of my site to the backend as a method to get some data from my JSON file. Please see the code below for clarification.
My NODE.JS Code:
function getData(filePath, cb){
var str = '';
fs.readFile(filePath, 'utf8', function(err, data){
if(err) throw err;
cb(data);
});
}
app.get("/api", (req, res)=>{
var userID = req.query.userID
getData('users.json', function(data) {
var processed = JSON.parse(data)
processed = processed.userID
console.log(processed)
res.send(processed)
console.log("completed request!")
} );
})
My JSON File
{
"RGdfei33":{
"img":"https://upload.wikimedia.org/wikipedia/commons/7/7c/Broken_Castor_Rex_Rabbit.JPG",
"bio":"I am a rex bunny",
"name": "Koos Konijn"
},
"RGdfei3":{
"img":"https://upload.wikimedia.org/wikipedia/commons/7/7c/Broken_Castor_Rex_Rabbit.JPG",
"bio":"I am monkey",
"name": ""
},
"FGdfei33":{
"img":"https://upload.wikimedia.org/wikipedia/commons/7/7c/Broken_Castor_Rex_Rabbit.JPG",
"bio":"I am a rex bunny",
"name": "Koos Konijn"
}}
Thanks Already!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|