我有一個問題,我想發布我的專案,但它不起作用..這是前端的代碼,但我會先解釋一下..所以基本上這里的程序是當我注冊我的帳戶時它會自動在我的貓鼬中添加到我的用戶和我的喜歡模型。更具體地說..我有這個東西
useEffect(() => {
axios.get('http://localhost:7171/users/')
.then(list => setlist(list.data))
.catch(err => console.log(err))
// console.log(list)
axios.get('http://localhost:7171/likes')
.then(listid => setlistid(listid.data))
.catch(err => console.log(err))
},[])
這個會讀取我創建的資料......然后當我注冊我的專案時,它會做這樣的事情......
const Register = e => {
axios.post('http://localhost:7171/users/register',e)
.then(res => console.log(res))
.catch(err => console.log(err))
}
//Something function submit here
if (name.length >= 6 && key.length >= 6) {
setCallError("Your Account has Registered")
Register(Inputs)
console.log(listid[0]._id,listid[1]._id,listid[2]._id,listid[3]._id,listid[4]._id)
axios.post(`http://localhost:7171/likes/item/${listid[0]._id}`,{ people:{name:name,key:key }})
.then(res => console.log(res.data))
.catch(err => console.log('Error:' err))
// axios.post('http://localhost:7171/likes/item/' listid[1]._id,{ people:{name:name,key:key }})
// .then(res => console.log(res.data))
// .catch(err => console.log('Error:' err))
// axios.post('http://localhost:7171/likes/item/' listid[2]._id,{ people:{name:name,key:key }})
// .then(res => console.log(res.data))
// .catch(err => console.log('Error:' err))
// axios.post('http://localhost:7171/likes/item/' listid[3]._id,{ people:{name:name,key:key }})
// .then(res => console.log(res.data))
// .catch(err => console.log('Error:' err))
// axios.post('http://localhost:7171/likes/item/' listid[4]._id,{ people:{name:name,key:key }})
// .then(res => console.log(res.data))
// .catch(err => console.log('Error:' err))
}
只是不要介意其他代碼功能,這只是如何向您展示它的處理程序只需專注于這件事
axios.post(`http://localhost:7171/likes/item/${listid[0]._id}`,{ people:{name:name,key:key }})
.then(res => console.log(res.data))
.catch(err => console.log('Error:' err))
I just comment out first my other post so that you can identify what I am doing here..cause I just want to post my 5 items at the same item..but there is a problem here. When I will try to submit this it will give me this kind of error in my post POST http://localhost:7171/likes/item/00000001a24dfc9b806e607f 400 (Bad Request) I don't understand why it is wrong I just passed it in id.. By the way this is my backend first
This code tells that I wanna reupdate my certain id since I want to append a new item in its list
people whenever I will register a user.
router.route('/item/:id').post((req,res) => {
const { likes, people } = req.body
const id = req.params.id
console.log(id)
if (!id) return res.status(400).json({ message: "missing id" });
Main.updateOne(
{ _id: (id) },
{
$set: {
likes,
},
$addToSet: { people: { $each: people } },
},
)
.then((likes) => res.json({ message: "New User Added" }))
.catch((err) => res.status(400).json("Error :" err));
})
but what I mean about here is that I passed the right id that I read in the data but I don't understand when I am trying to post it in the id that reads the data.. as you see that listid[0]._id it means I want to pass my id that I created but i don't understand why it can't post the items...but when I am trying to create it in my postman like this. It is working very well..Do you guys have any idea?? Or notice wrong at my codes I don't see it anywhere.

EDITED
Here is the console.log(listid)

And here is for the backend part

uj5u.com熱心網友回復:
該錯誤說$each將陣列作為引數,但people在從客戶端發出請求時似乎沒有定義。您應該像在 Postman 中一樣將人員添加到您的 Axios 請求正文中。或者,如果 people 未定義,您可以傳遞一個空陣列:
Main.updateOne({
_id: (id)
}, {
$set: {
likes,
},
$addToSet: {
people: {
$each: people || [] // empty array if people is undefined
}
},
}, )
.then((likes) => res.json({
message: "New User Added"
}))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447609.html
標籤:node.js mongodb express postman backend
上一篇:找不到路由模塊
