【问题标题】:I can not write data from the Firebase Cloud Functions in Firebase Firestore我无法从 Firebase Firestore 中的 Firebase Cloud Functions 写入数据
【发布时间】:2018-02-07 21:48:09
【问题描述】:

几天前我开始使用 Firebase Cloud Functions,但无法将数据写入数据库。 你可以帮帮我吗。 提前致谢。

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.userCreated = functions.auth.user().onCreate(event => {
    let userId = event.data.uid;
    let userName = event.data.displayName;

    admin.firestore()
    .collection("users")
    .doc(userId)
    .set({
        name: userName,
        city: "",
        rating: 0
    });
});

【问题讨论】:

    标签: javascript firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

    see the documentation 了解 Cloud Functions 如何处理异步代码。

    你没有从你的函数中返回一个承诺。如果您的函数执行任何产生承诺的异步工作,您必须通过返回工作完成时解决的承诺来指示 Cloud Functions 等待该工作完成:

    exports.userCreated = functions.auth.user().onCreate(event => {
        let userId = event.data.uid;
        let userName = event.data.displayName;
    
        return admin.firestore()
        .collection("users")
        .doc(userId)
        .set({
            name: userName,
            city: "",
            rating: 0
        });
    });
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 2020-02-01
    • 2020-02-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2018-08-11
    • 2018-10-16
    相关资源
    最近更新 更多