Javascript 删除JSON文件中的元素

Javascript 删除JSON文件中的元素,javascript,node.js,json,xmlhttprequest,crud,Javascript,Node.js,Json,Xmlhttprequest,Crud,我刚刚开始我的第一个项目,在学习如何编码的路上。我已将所有用户保存在一个名为storage.JSON的JSON文件中,并且我拥有以“currentUser”的名称登录到本地存储的用户的信息。我试图创建一个按钮,用户可以从JSON文件中挖掘自己。当我在整个过滤过程中都使用控制台日志时,它会起作用。在我看来,问题在于无法捕获if语句之外的allUsers变量。有人能帮我解决这个问题吗?这样我就可以在函数结束时将诱惑者发送回API 以下是我在API中的代码: app.delete('/deletePr

我刚刚开始我的第一个项目,在学习如何编码的路上。我已将所有用户保存在一个名为storage.JSON的JSON文件中,并且我拥有以“currentUser”的名称登录到本地存储的用户的信息。我试图创建一个按钮,用户可以从JSON文件中挖掘自己。当我在整个过滤过程中都使用控制台日志时,它会起作用。在我看来,问题在于无法捕获if语句之外的allUsers变量。有人能帮我解决这个问题吗?这样我就可以在函数结束时将诱惑者发送回API

以下是我在API中的代码:

app.delete('/deleteProfile', (req, res) => {
    
    var allUsers = JSON.parse(fs.readFileSync("storage.JSON"))
    res.json(allUsers)

})

app.post('/deleteProfile', (req, res)=> {
    let reqData = req.body;
    console.log('Post request virker')
    console.log(reqData) 
    var storage = JSON.parse(fs.readFileSync("storage.JSON"))
    storage.push(reqData);
    fs.writeFileSync("storage.JSON", JSON.stringify(storage, null, 2));

    //console.log(reqData);
    res.send(JSON.stringify({mesagge: 'This user has been delete from', storage}));
})
deleteUser.js中的代码

deleteUser = document.getElementById("deleteBtn")


deleteUser.addEventListener('click', function() {
    
        const xhr = new XMLHttpRequest();
        xhr.responseType = "json"
    
        xhr.addEventListener("readystatechange", function() {
        if(this.readyState === 4) {
            var allUsers = this.response;
            console.log(allUsers);

      
            
          let currentUser = JSON.parse(localStorage.getItem("currentUser"))

            allUsers = allUsers.filter(allUser => allUser.username !== currentUser.username);
            console.log(allUsers);

        } });
                       

        xhr.open("DELETE", "http://localhost:2500/deleteProfile", true);
            
        xhr.setRequestHeader("Content-Type", "application/json");
            
      
        xhr.send(JSON.stringify(allUser));
    })

您应该在if块范围外定义
alluser
变量,并在if块中设置该数据。 请在
deleteUser.js
中尝试此选项:

    deleteUser = document.getElementById("deleteBtn")
    
    
    deleteUser.addEventListener('click', function() {
            let allUsers;
            const xhr = new XMLHttpRequest();
            xhr.responseType = "json"
        
            xhr.addEventListener("readystatechange", function() {
            if(this.readyState === 4) {
                allUsers = this.response;
          
                
              let currentUser = JSON.parse(localStorage.getItem("currentUser"))
    
                allUsers = allUsers.filter(allUser => allUser.username !== currentUser.username);
    
            } });
                           
    
            xhr.open("DELETE", "http://localhost:2500/deleteProfile", true);
                
            xhr.setRequestHeader("Content-Type", "application/json");
                
          
            xhr.send(JSON.stringify(allUser));
        })


感谢您的帮助@alefseen:-)您知道当我从JSON文件获取数据并随后使用post更新JSON文件时,我在第一次请求中使用delete是否正确吗。因为现在函数运行并在I console.log时删除用户,但它不会更新JSON文件。我认为您在express中的删除请求不会发送任何响应。如果为true,请在设置json data`res.json(allUsers).send()`之后调用send函数,然后在应用程序中只需将删除响应替换为旧数据谢谢。当您说我对旧数据给出了替换删除响应时,您能详细说明一下吗?这可以帮助您: