Firebase Cloud Firestore将文档引用存储为字符串而不是对象引用

时间:2018-12-07 03:46:13

标签: firebase google-cloud-firestore

我有一个集合营地,应该将注释文档的引用存储为对象引用的数组 我是这样做的

try {
    commentRef.add(newComment).then(ref => {
        console.log("success COMMENT ADDED");
        var refForThisComment = ref.id;
        docRef.update({
            comments: firebase.firestore.FieldValue.arrayUnion(
                "/campgrounds/" + refForThisComment
            )
        });
        res.redirect("/campgrounds");
    });
} catch (error) {
    res.send(error);
}

但是在控制台中,当我检查文档引用数组是字符串数组时!

1 个答案:

答案 0 :(得分:0)

您看到一个字符串数组,因为您将字符串传递给了arrayUnion

"/campgrounds/" + refForThisComment

这不仅成为文档参考。字符串串联总是在JavaScript中产生一个字符串。

如果要引用文档,请改为传递DocumentReference对象:

comments: firebase.firestore.FieldValue.arrayUnion(ref)
相关问题