firebase v.9 deletedoc()不做任何事情或遇到任何错误
我是React和Firebase的新手,并且我遇到了一个问题,即删除文档功能未被调用并且没有捕获错误。函数deleteitemhandler()确实运行,但是deletedoc()却没有。我已经更改了规则,并确保导入是正确的,但它仍然行不通。
deleteitemhandler(),app.jsx
//? Delete item function
const deleteItemHandler = async (id) => {
try {
await deleteDoc(doc(db, "items", id));
}
catch (error) {
console.log(error);
}
}
删除按钮,yourcard.jsx
<button
className="btn btn-danger ms-2"
onClick={() => {
// console.log(props.name)
props.deleteItemHandler(props.id)
}}
>
Delete
</button>
注意更新: 道具通过 app.jsx to userIdentity.jsx
<UserIdentity
className="userId"
currentUser={username}
loginHandler={loginHandler}
logoutHandler={logoutHandler}
addListingHandler={addListingHandler}
//? for YourCard
itemsFromDB={itemsFromDB}
deleteItemHandler={deleteItemHandler}
/>
userIdentity.jsx to yourcard.jsx
{yourItems.map((item) => {
return (
<YourCard
// className="YourCard"
name={item.name}
id={item.id}
key={item.id}
deleteItemHandler={props.deleteItemHandler}
/>
);
})}
I'm new to React and Firebase, and I got a problem where the delete document function doesn't get called and no errors were caught. The function deleteItemHandler( ) does run, but the deleteDoc( ) doesn't. I have changed rules and make sure the imports are correct, yet it still won't work.
deleteItemHandler( ), App.jsx
//? Delete item function
const deleteItemHandler = async (id) => {
try {
await deleteDoc(doc(db, "items", id));
}
catch (error) {
console.log(error);
}
}
Delete Button, YourCard.jsx
<button
className="btn btn-danger ms-2"
onClick={() => {
// console.log(props.name)
props.deleteItemHandler(props.id)
}}
>
Delete
</button>
Note update:
props passing
App.jsx to UserIdentity.jsx
<UserIdentity
className="userId"
currentUser={username}
loginHandler={loginHandler}
logoutHandler={logoutHandler}
addListingHandler={addListingHandler}
//? for YourCard
itemsFromDB={itemsFromDB}
deleteItemHandler={deleteItemHandler}
/>
UserIdentity.jsx to YourCard.jsx
{yourItems.map((item) => {
return (
<YourCard
// className="YourCard"
name={item.name}
id={item.id}
key={item.id}
deleteItemHandler={props.deleteItemHandler}
/>
);
})}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终发现问题在于
adddoc()
,它添加了yc2
键入deletedoc()
使用搜索文档的新文档。 MC2
类型。将添加方法更改为setDoc()
将其修复。Finally figured out that the problem lies on
addDoc()
, which adds new document withyc2
type whiledeleteDoc()
searches documents withmc2
type. Changed the add method tosetDoc()
fixes it.我以为您没有正确处理
道具
im thinking you're not handling props correctly
child