![](/img/trans.png)
[英]Unhandled Rejection (TypeError): Cannot read property 'id' of undefined in stripe.confirmCardPayment in React.js
[英]MongoDB / Axios / React.js : Cannot read property 'id' of undefined"
我嘗試刪除一個項目,我在谷歌上搜索了一個解決方案,我與其他帖子進行了比較,我也嘗試了“findByOneAndRemove”和 PostMan,但沒有奏效。
我不明白,因為我在使用 console.log 的請求中看到了 id
你能幫助我嗎 ?
服務器.js:
app.delete("/argonautes/:id", async (res, req, next) => {
try {
await db.Argonaute.findOneAndDelete({_id : req.params.id})
return success(res, "Argonaute deleted !" )
} catch (err) {
next({ status: 400, message : "Failed to delete an Argonaute" + err})
}
});
應用程序js
const deleteArgonaute = async (id) => {
axios.delete(`/argonautes/${id}`)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
})
}
組件.js
<section className="w-full flex flex-col h-64 p-2 flex-wrap relative">
{argonautes.map( ({_id, name}) => (
<div className="shadow-lg p-2 mx-auto rounded my-2 animate-scale relative" key={_id}>
{name}
<FaMinusCircle className="absolute cursor-pointer top-0 right-0 text-gray-500" onClick={() => deleteArgonaute(_id)}/>
</div>
))}
</section>
提前致謝 :)
您的 app.delete 代碼看起來不錯,我確實測試過並且有效。 有時可能發生的事情(發生在我身上一次)是您可能會在 id params 字符串中收到一個額外的不可見字符。
您可以通過在 deleteArgonaute() 上硬粘貼 _id 來測試它,如果它有效,則意味着您需要檢查您真正收到的內容。
為我添加了一個 id.trim()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.