Понятия не имею, но кажется, что, когда я пытаюсь получить атрибут элемента из файла JSON, он говорит, что он нулевой. Все еще есть проблемы аудита npm :(, что вы думаете об этом?
Вот отредактированный код, который я сделал до сих пор:
export const data = require('./file.json');
export let DATA = data as Type[];
let temp = DATA;
app.post('/api/tickets', (req, res) => {
// load past data into json string
const past_data = JSON.stringify(temp);
// load new data into json string
const new_element = JSON.stringify(req.params.formData)
if (new_element !== "")
{
// concat both string to 1 json string, then write into fs
fs.writeFile("./file.json",[past_data,new_element],(err) => {
if (err) throw err;
});
}
// send it back as response to request
const new_data = JSON.parse([past_data,new_element].toString());
res.send(new_data);
});
app.delete('/api/tickets/:id', (req,res) => {
// fined requested ticket based on id in global temp
const ticket = temp.find(t => t.id === (req.params.id));
if (typeof ticket !== 'undefined') {
const index = temp.indexOf(ticket);
// remove it from global temp
temp.splice(index, 1)
}
// create json string out of global temp
const data_after_delete = JSON.stringify(temp);
// write it straight into fs
fs.writeFile("./file.json",data_after_delete,(err) => {
if (err) throw err;
});
// send it back to requester
const new_data = JSON.parse(data_after_delete);
res.send(new_data);
});
Один объект из файла json перед тем, как я в него напишу:
[
{
"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": "jug@nesetal.af",
"creationTime": 1542111235544,
"labels": ["Corvid", "Api"]
},
Один объект из файла json после того, как я в него напишу:
["[\"[\\\"[\\\\\\\"[{\\\\\\\\\\\\\\\"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\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"jug@nesetal.af\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"creationTime\\\\\\\\\\\\\\\":1542111235544,\\\\\\\\\\\\\\\"labels\\\\\\\\\\\\\\\":[\\\\\\\\\\\\\\\"Corvid\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Api\\\\\\\\\\\\\\\"]},