It appears that I am facing an issue where trying to access an attribute of an element from a JSON file returns null. Additionally, I am still encountering npm audit problems. What are your thoughts on this situation?
Below is the code snippet that has been edited:
export const data = require('./file.json');
export let DATA = data as Type[];
let temp = DATA;
app.post('/api/tickets', (req, res) => {
// Load previous data into a JSON string
const past_data = JSON.stringify(temp);
// Load new data into a JSON string
const new_element = JSON.stringify(req.params.formData)
if (new_element !== "") {
// Concatenate both strings into one JSON string and write it into fs
fs.writeFile("./file.json",[past_data,new_element],(err) => {
if (err) throw err;
});
}
// Send the response back with the new data
const new_data = JSON.parse([past_data,new_element].toString());
res.send(new_data);
});
app.delete('/api/tickets/:id', (req, res) => {
// Find the requested ticket based on id in the global temp
const ticket = temp.find(t => t.id === (req.params.id));
if (typeof ticket !== 'undefined') {
const index = temp.indexOf(ticket);
// Remove the ticket from the global temp
temp.splice(index, 1)
}
// Create a JSON string out of the modified global temp
const data_after_delete = JSON.stringify(temp);
// Write the data directly into fs
fs.writeFile("./file.json",data_after_delete,(err) => {
if (err) throw err;
});
// Send the updated data back to the requester
const new_data = JSON.parse(data_after_delete);
res.send(new_data);
});
One object from the json file before any modification:
[
{
"id": "81a885d6-8f68-5bc0-bbbc-1c7b32e4b4e4",
"title": "Need a Little Help with Your Site? Hire a Corvid Web Developer",
"content": "Here at Wix we strive to support you with this community forum, API references, articles, videos and code examples. But sometimes you might need a little extra help to get your site exactly the way you want it. \nHire a developer from the Wix Arena, an online marketplace with top Corvid web developers from around the world. Submit your project details here, and we’ll find the right professional for you.",
"userEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7ddc2d0f7d9d2c4d2c3d6db99d6d1">[email protected]</a>",
"creationTime": 1542111235544,
"labels": ["Corvid", "Api"]
},
One object from the json file after being modified:
["[\"[\\\"[\\\\\\\"[{\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"81a885d6-8f68-5bc0-bbbc-1c7b32e4b4e4\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"title\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Need a Little Help with Your Site? Hire a Corvid Web Developer\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"content\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Here at Wix we strive to support you with this community forum, API references, articles, videos, and code examples. Sometimes, additional assistance may be required to align your site precisely as desired. \\\\\\\\\\\\\\\\nHire a developer from the Wix Arena, an online marketplace featuring top Corvid web developers worldwide. Share your project details here, and we will connect you with the ideal professional.\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"userEmail\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e140b193e101b0d1b0a1f12501f18">[email protected]</a>\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"creationTime\\\\\\\\\\\\\\\":1542111235544,\\\\\\\\\\\\\\\"labels\\\\\\\\\\\\\\\":[\\\\\\\\\\\\\\\"Corvid\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Api\\\\\\\\\\\\\\\"]},