【问题标题】:firebase cloud function return json object from batch functionfirebase 云函数从批处理函数返回 json 对象
【发布时间】:2019-10-20 19:52:29
【问题描述】:

我想从批处理操作返回到客户端 Json 对象 {mesage: "this is what I want to return"}...我错过了什么在这里?

exports.addMessage3 = functions.https.onCall(async (data, context) => {
    const db = admin.firestore();

    const query = db
        .collection('smtg')
        .where('status', '==', false)
        .limit(1);

    return db
        .runTransaction(async transaction => {
            return transaction
                .get(query)
                .then((querySnapshot) => {
                    const snapshot = querySnapshot.docs[0];

                    if (typeof snapshot === "undefined") {
                        console.log('XXXXXXXX!  ', snapshot);
                        const xyz = myFunction()

                        console.log('XXXXXXXXZZZZZZ!  ', xyz);
                        return { error: "undefined" };
                    }
                    const data = snapshot.data();
                    console.log('LLLoooog!  ', data);
                    transaction.update(snapshot.ref, { status: true });
                    return data;
                })
        })
        .then((data) => {
            console.log('Transaction successfully committed!  ', data);
            return data;
        })
        .catch((error) => {
            console.log('Transaction failed:  ', error);
            return error;
        });
});




async function myFunction() {
    const db = admin.firestore();
    let batch = admin.firestore().batch();

    let nycRef = db.collection('smtg').doc();
    batch.set(nycRef, { combination: 'ABC11', status: false });

    return batch.commit().then(function () {
        console.log('Batched.');
        return {mesage: "this is what I want to return"}
    });
}

如果 snapshot 未定义,我想返回 myFunction() 而不返回 { error: "undefined" }

【问题讨论】:

  • 能否请您添加您的云函数的整个代码,而不仅仅是云函数调用的函数的代码。
  • @RenaudTarnec 希望能更好地了解我想要实现的目标......
  • 我认为您不能在事务中间调用返回 batch.commit() 的函数。您只能使用事务的方法:googleapis.dev/nodejs/firestore/latest/Transaction.html。此外,通过阅读您的代码来理解您的确切目标有点困难。您可以在代码中添加一些 cmets 以帮助我们了解您的目标。另外,为什么你只使用一个文档的批量写入?
  • 天哪……你是对的……我到底在想什么?我完全失去了我在交易中间的感觉……但是,batch.commit() 确实有效,它确实创建了新实例……真正的问题是我所做的是否可行且有效!
  • 顶级回调返回的promise必须解析为你要发送给客户端的数据。

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

发现我不能在事务中间调用批处理,我可以从 .then((data) 块调用它。所以我所做的是:

exports.addMessage3 = functions.https.onCall(async (data, context) => {
    const db = admin.firestore();

    const query = db
        .collection('smtg')
        .where('status', '==', false)
        .limit(1);

    return db
        .runTransaction(async transaction => {
            return transaction
                .get(query)
                .then((querySnapshot) => {
                    const snapshot = querySnapshot.docs[0];
                    if (typeof snapshot === "undefined") {
                        return snapshot;
                    }
                    const data = snapshot.data();
                    console.log('LLLoooog!  ', data);
                    transaction.update(snapshot.ref, { status: true });
                    return data;
                })
        })
        .then((data) => {
            console.log('Transaction successfully committed!  ', data);
            if (typeof data === "undefined") {
                    return myFunction();
            }
            return data;
        })
        .catch((error) => {
            console.log('Transaction failed:  ', error);
            return error;
        });
});




async function myFunction() {
    const db = admin.firestore();
    let batch = admin.firestore().batch();

    let nycRef = db.collection('smtg').doc();
    batch.set(nycRef, { combination: 'ABC11', status: false });

    return batch.commit().then(function () {
        console.log('Batched.');
        return {mesage: "this is what I want to return"}
    });
}

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2018-08-04
    • 2019-05-10
    • 1970-01-01
    相关资源
    最近更新 更多