英文:
JSON server - post data into specific object in array
问题
以下是您提供的内容的翻译部分:
第一步中,我将数据发布到'http://localhost:3003/users',一切正常:
(async () => {
const res = await fetch('http://localhost:3003/users', {
method: 'POST',
body: JSON.stringify({ name, password }),
headers: {
'Content-Type': 'application/json'
}
});
但在第二步中,我有以下代码:
useEffect(() => {
(async () => {
try {
const resp = await fetch(`http://localhost:3003/users/${userID}`, {
method: 'POST',
body: JSON.stringify({ tasks: [1, 2, 3] }),
headers: {
'Content-Type': 'application/json'
}
}).then(res => console.log(res));
} catch (err) {
console.error(err);
}
})();
}, [userID]);
在控制台中记录后,得到了以下结果:
Response {type: "cors", url: "http://localhost:3003/users/0", redirected: false, status: 404, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "cors"
url: "http://localhost:3003/users/0"
但是,当我访问'http://localhost:3003/users/0'时,我能看到用户,所以似乎URL有效。当我删除${userID}
时,它可以工作,但数组被添加为最后一个对象,而不是特定用户的对象。此外,'http://localhost:3003/users/${userID}' 适用于GET方法。
问题是 - 如何将数据发布到JSON服务器中数组中的单个对象?
db.json:
{
"users": [
{
"id": 0,
"name": "Ola",
"password": "12345"
},
{
"name": "newuser",
"password": "newuser",
"id": 1
}
]
}
表单提交检查是否存在具有此名称的用户;如果不存在,则发布新用户。
英文:
I'm practicing fetch api and have problem with posting data... in first step I post to 'http://localhost:3003/users' and it works fine -> code:
(async () => {
const res = await fetch('http://localhost:3003/users', {
method: 'POST',
body: JSON.stringify({ name, password }),
headers: {
'Content-Type': 'application/json'
}
});
but in another component (second step) I have this code:
useEffect(() => {
(async () => {
try {
const resp = await fetch(`http://localhost:3003/users/${userID}`, {
method: 'POST',
body: JSON.stringify({ tasks: [1, 2, 3] }),
headers: {
'Content-Type': 'application/json'
}
}).then(res => console.log(res));
} catch (err) {
console.error(err);
}
})();
}, [userID]);
and after logging to the console, got this:
Response {type: "cors", url: "http://localhost:3003/users/0", redirected: false, status: 404, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "cors"
url: "http://localhost:3003/users/0"
But when I go to http://localhost:3003/users/0, I can see the user so it seems that url works... Of course when I delete ${userID} it works, but array is added as the last object, not to this specific user... What's more - http://localhost:3003/users/${userID} works with GET method.
The question is - how to post data to single object in array in JSON server??
db.json:
{
"users": [
{
"id": 0,
"name": "Ola",
"password": "12345"
},
{
"name": "newuser",
"password": "newuser",
"id": 1
}
]
}
form submission checks if there is a user with this name; if not, post new user
答案1
得分: 1
已解决
我使用了错误的方法 - 使用 POST 而不是 PATCH
英文:
RESOLVED
I used wrong method - POST instead of PATCH
答案2
得分: 0
So when you are posting something, in your case you are posting it to the users array
so it should be try in this way
fetch(http://localhost:3003/users
)
and in your body you need to pass the id
{ id:${userId}
tasks: [1, 2, 3] }
Update these things and check.
OR
Update
As it is adding a new property to the user object need to use Patch instead of Post
You can read more on here
英文:
So when you are posting something, in your case you are posting it to the users array
so it should be try in this way
fetch(`http://localhost:3003/users`)
and in your body you need to pass the id
{ id:`${userId}` tasks: [1, 2, 3] }
Update these things and check.
OR
Update
As it is adding a new property to the user object need to use Patch instead of Post
You can read more on here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论