Asynchronous https firebase functions

后端 未结 4 1043
一整个雨季
一整个雨季 2021-01-15 06:15

Should HTTPS functions return asynchronous promises like realtime functions have to? We haven\'t been returning in HTTPS functions (just using res.status.send etc), and it l

4条回答
  •  既然无缘
    2021-01-15 06:32

    Your cloud functions should return"end" with either of the following

    res.redirect(), res.send(), or res.end()

    What they mean by returning promises, is lets imagine you have a cloud function that updated a node in your realtime database, you would like to complete that work before responding to the HTTP request.

    Example code

    let RemoveSomething = functions.https.onRequest((req, res) => {
        cors(req, res, () => {
            // Remove something
            DoDatabaseWork()
                .then(function (result) {
                    res.status(200).send();
                })
                .catch(function (err) {
                    console.error(err);
                    res.status(501).send();
                });
        });
    });
    

    Update: Added DoDatabaseWork example.

    const DoDatabaseWork = function () {
        return new Promise(function (resolve, reject) {
            // Remove SomeNode
            admin.database().ref('/someNode/').remove()
                .then(function (result) {
                    resolve();
                })
                .catch(function (err) {
                    console.error(err);
                    reject();
                });
        });
    }
    

提交回复
热议问题