表达不了解的帖子和删除回复的概念

时间:2019-10-19 17:01:56

标签: javascript express

我不知道,但是似乎当我尝试从JSON文件进入某个元素的属性时,它说它为null。 仍然有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\\\\\\\\\\\\\\\"]},

1 个答案:

答案 0 :(得分:0)

仅在写入文件时使用JSON.stringify,而在从文件读取时仅使用JSON.parse(如果您不使用require进行隐式解析)。将数据作为普通对象和数组而不是JSON字符串进行处理-只会破坏您注意到的结构。

export let DATA: Type[] = require('./file.json');
function save() {
    const jsonString = JSON.stringify(DATA);
//                     ^^^^^^^^^^^^^^^^^^^^^ only call it here
    fs.writeFile("./file.json", jsonString, (err) => {
        if (err) throw err;
    });
}

app.post('/api/tickets', (req, res) => {
    if (req.params.formData) {
        const new_element = req.params.formData; // might need a JSON.parse() if it's a a json string
        // add to global temp by array manipulation
        DATA.push(new_element);
        save();
    }
    // send it back as response to request
    res.send(DATA);
});

app.delete('/api/tickets/:id', (req,res) => {
    // find requested ticket based on id in global temp
    const ticket = DATA.findIndex(t => t.id === (req.params.id));
    if (ticket !== -1) {
        // remove it from global temp
        DATA.splice(index, 1);
        save();
    }
    // send it back to requester
    res.send(DATA);
});