firestore在此集合操作中不返回结果

flseospp  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(263)

正在阅读文档,但仍然无法使其工作。
在代码中 docRef 即使设置操作成功,也未定义。
为什么,我正期待着 docRef 就是我刚才设定的值。

/**
 * Create new Tag
 * @param {name} name
 * @param {category} category
 */
export function saveNewTag(name, category) {
    return (dispatch, getState, firebase) => {
        dispatch(addNewTagStart());
        firebase.db
            .collection(FIRESTORE.GLOBAL_TAGS)
            .doc(name)
            .set({ [FIRESTORE.TAG_NAME]: name, [FIRESTORE.CATEGORY]: category }, { merge: true })
            .then(docRef => {
                // docRef is undefined here
                dispatch(addNewTagSuccess(docRef));
            })
            .catch(error => {
                dispatch(addNewTagFailure(error));
            });
    };
}

显示未定义属性的图像 docRef

hrirmatl

hrirmatl1#

消防商店 set() 操作返回一个 Promise<void> ,所以你不能得到 DocumentReference 从那里开始。
相反,您可以将 DocumentReference 在一个变量中,然后从那里重新使用它。比如:

let docRef = firebase.db
    .collection(FIRESTORE.GLOBAL_TAGS)
    .doc(name);

docRef.set({ [FIRESTORE.TAG_NAME]: name, [FIRESTORE.CATEGORY]: category }, { merge: true })
    .then(() => {
        dispatch(addNewTagSuccess(docRef));
    })
    .catch(error => {
        dispatch(addNewTagFailure(error));
    });

相关问题