未捕获(承诺中)Firebase错误:集合引用无效,集合引用的段数必须为奇数

d6kp6zgx  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(130)

我收到错误

Uncaught (in promise) FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but 
Accounts/sKnjAQzJ1sQ5qRC4ki6b/RequestsReceived/m27vTB22izq6UixWP2s6 has 4.

我的密码是

var ref2 = collection(db, "Accounts/" + localStorage.getItem('Id') + "/RequestsReceived", id);
    const docRef2 = await getDoc(ref2);
    await deleteDoc (ref2);

我要删除的文档的URL为/Accounts/sKnjAQzJ1sQ5qRC4ki6b/RequestsReceived/m27vTB22izq6UixWP2s6
我知道如果你添加一个文档,它必须有奇数个路径,集合名称,或者集合/文档/集合。但是deleteDoc不应该有偶数个数量,因为我正在删除集合/文档或者集合/文档/集合/文档。我做错了什么?

xuo3flqw

xuo3flqw1#

"Accounts/" + localStorage.getItem('Id') + "/RequestsReceived", id
此路径指向“RequestsReceived”子集合中的文档。因此应使用doc()创建DocumentReference。
此外,只要您有引用,就不需要在删除之前获取文档:

// ensure all values are defined
console.log(localStorage.getItem('Id'), id);

// use doc()
var ref2 = doc(db, "Accounts/" + localStorage.getItem('Id') + "/RequestsReceived", id);

await deleteDoc(ref2);

相关问题