如何使用fetch更新数据库中的JSON文件

huangapple go评论59阅读模式
英文:

How to update JSON file in database using fetch

问题

我目前有这个函数将JSON文件添加到数据库中:

const history = useNavigate();
function addLoggbokHandler(loggbokData){
    fetch(
        "api/objects.json",
        {
            method: "POST",
            body: JSON.stringify(loggbokData),
            headers:{
                "Content-Type": "application/json"
            }
        }
    ).then(() =>{
        history(0);
    });
}

以及这个函数来从数据库中获取所有项目以进行渲染:

const[isLoading, setIsLoading] = useState(true);
const[loadedLoggboks, setLoadedLoggboks] = useState([]);

useEffect(() => {
    setIsLoading(true);
    fetch(
        "api/objects.json",
    ).then(response => {
        return response.json();
    }).then(data => {
        const loggboks = [];

        for(const key in data)
        {
            console.log(key);
            console.log(data);
            const loggbok = {
                id: key,
                ...data[key]
            };

            loggboks.push(loggbok);
        }

        setIsLoading(false);
        setLoadedLoggboks(loggboks);
    });
}, [])

如果我想使用ID与数据库中指定的JSON文件进行交互,以更新/更改其值,我可以执行以下操作。欢迎任何帮助:

(Note: The following code is an explanation of how to interact with a specified JSON file in the database using its ID.)

// 假设你有一个表示要更新的JSON文件的ID
const fileIdToUpdate = "your_file_id_here";

// 准备要发送的新数据
const updatedData = {
    // 更新的属性和值
    // 例如:name: "New Name", description: "New Description"
};

// 使用fetch发送PUT请求以更新JSON文件
fetch(`api/objects/${fileIdToUpdate}.json`, {
    method: "PUT",
    body: JSON.stringify(updatedData),
    headers: {
        "Content-Type": "application/json"
    }
}).then(response => {
    if (response.ok) {
        // 更新成功
        // 执行任何必要的操作
    } else {
        // 更新失败
        // 处理错误
    }
});

在这个示例中,你需要替换fileIdToUpdate为要更新的JSON文件的实际ID,并准备要更新的数据。然后,使用fetch发送PUT请求来更新JSON文件。在成功或失败时,你可以执行适当的操作。

英文:

I currently have this function to add a JSON file to the database :

  const history = useNavigate();
    function addLoggbokHandler(loggbokData){
        fetch(
            "api/objects.json",
            {
                method: "POST",
                body: JSON.stringify(loggbokData),
                headers:{
                    "Content-Type": "application/json"
                }
            }
            ).then(() =>{
                history(0);
            });
    }

and this function to get all the items from the database to render

 const[isLoading, setIsLoading] = useState(true);
 const[loadedLoggboks, setLoadedLoggboks] = useState([]);

    useEffect(() => {
        setIsLoading(true);
        fetch(
            "api/objects.json",
        ).then(response => {
            return response.json();
        }).then(data => {
            const loggboks = [];

            for(const key in data)
            {
                console.log(key);
                console.log(data);
                const loggbok = {
                    id: key,
                    ...data[key]
                };

                loggboks.push(loggbok);
            }

            setIsLoading(false);
            setLoadedLoggboks(loggboks);
        });
    }, [])

What can I do if I want to interact with specified JSON file in the database using id, to update/change its values? any help is welcome

答案1

得分: 0

这是更新函数的代码部分。

function updatebokHandler(id, loggbokData) {
  fetch(`api/objects/${id}.json`, {
    method: "PUT",
    body: JSON.stringify(loggbokData),
    headers: {
      "Content-Type": "application/json"
    }
  }).then(() => {
    // 在更新成功后执行任何必要的操作
  });
}

根据您的需求更改端点和数据。

英文:

Here is the code for update function.

function updatebokHandler(id, loggbokData) {
  fetch(`api/objects/${id}.json`, {
    method: "PUT",
    body: JSON.stringify(loggbokData),
    headers: {
      "Content-Type": "application/json"
    }
  }).then(() => {
    // Perform any necessary actions after the update is successful
  });
}

Change endpoints and data according to your needs

huangapple
  • 本文由 发表于 2023年6月1日 15:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定