繁体   English   中英

设置后如何取回文件,firebase?

[英]How to get back document after set, firebase?

设置新的文档 ID 后如何取回文档 ID?

我有一些尝试,但这是我最新的:

handleCreateCard = async (name, token) => {
    try {
        const new_card = await firestore()
          .collection('Cards')
          .doc()
          .set({
            user_id: user.uid,
            name: name,
            token: token,
          })
          .then(function(docRef) {
              console.log("Document written with ID: ", docRef.id);
          })
          .then(() => this.props.navigation.navigate("CardDetail", {cardId: 2}));

//              console.log("ATTEMPT !", handleCreateCard);
//              console.log("ATTEMPT 2", new_card);
        } catch (error) {
            console.log(error.toString(error));
        }
    }

docRef行给我错误:

错误: TypeError: null is not an object (evaluating 'docRef.id')

记录 handleCreateCard 错误: ReferenceError: Can't find variable: handleCreateCard

然后是new_card :“这是未定义的”

尝试做:

.then(docRef => {
    console.log("Document written with ID: ", this.id); // also tried docRef.id
})

我得到Document written with ID: undefined with docRef.id: TypeError: null is not an object (evaluating 'docRef.id')

该文档也在创建中,所以不确定为什么它没有回来

我尝试了几种方法来做到这一点,然后还尝试了上面的所有方法: Firestore - How to get document id after adding a document to a collection

不知道为什么它不起作用。

有什么帮助吗?

如 API 文档中所述,不带参数的doc()立即返回已包含 ID 的DocumentReference

const docRef = await firestore()
    .collection('Cards')
    .doc()
const id = docRef.id
docRef
    .set(...)
    .then(/* use the id here if you want */)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM