我有一个代码,在该代码中,我在reactjs中遍历数组中的元素。对于每个元素,我都有一个delete按钮,它应该从db中删除该元素。编译时,代码引发以下错误:
Uncaught :无法读取未定义的属性(读取'indexOf')
这是我的联署材料:
{(zakazani.map((termin, key) => (
<tr key={key+1} className="border-t-2 border-b-2 py-4 bg-slate-100 border-slate-200">
<td className="pl-4 border-r-2 border-slate-200">{key+1}</td>
<td className="pl-4 border-r-2 border-slate-200">{termin.name}</td>
<td className="pl-4 border-r-2 border-slate-200">{termin.phone}</td>
{(termin.time >= 1000) ? (
<td className="pl-4 border-r-2 border-slate-200">
{Math.floor(termin.time / 100)}:{termin.time % 100} - {((termin.time % 100 >= 15) ? Math.floor(termin.time / 100) + 1 : Math.floor(termin.time / 100))}:{(termin.time % 100 + 45) % 60}
</td>
) : (
<td className="pl-4 border-r-2 border-slate-200">
{Math.floor(termin.time / 100)}:{termin.time % 100} - {((termin.time % 100 >= 15) ? Math.floor(termin.time / 100) + 1 : Math.floor(termin.time / 100))}:{(termin.time % 100 + 45) % 60}
</td>
)}
<td className="pl-2">
<button
onDoubleClick={deleteOne(termin.time, termin.id)}
className="text-white bg-sky-600 w-10 h-10 flex justify-center px-1 pt-3 rounded-sm mt-2 mb-1 shadow-sm shadow-sky-200"><FaTrash /></button>
</td>
</tr>
)))}下面是我的deleteOne函数:
const deleteOne = (time, uid) => {
const ref = doc(db, 'zakazeniTermini', uid)
const ref2 = doc(db, 'termini', time.toString())
deleteOne(ref)
updateDoc(ref2, {
slobodniKreveti: increment(1)
})
const newArray = zakazani.filter(termin => termin.id !== uid)
setZakazani(newArray)
}编辑:函数中的两个参数都有错误。
(预先谢谢你的帮助:)
发布于 2022-08-18 22:05:12
这意味着将null (或undefined)传递到doc调用的某个地方。很可能是您的uid变量是null,但是在使用之前您应该检查所有变量:
const deleteOne = (time, uid) => {
if (!uid) throw `deleteOne requires a uid value, but you passed '${uid}'.`
if (!time || !time.toString) throw `deleteOne requires a time value that implements toString(), but you passed '${time}'.`
...
}https://stackoverflow.com/questions/73408719
复制相似问题