firebase云函数从批处理函数返回json对象

问题描述 投票:0回答:1

我想从批处理操作返回到客户端Json对象{消息:“这是我要返回的内容”}从批处理操作开始……我在这里缺少什么?

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"}
    });
}

我未定义快照的情况,我想返回myFunction()而不返回{error:“ undefined”}

node.js firebase google-cloud-firestore google-cloud-functions
1个回答
0
投票

[发现无法在事务中间调用批处理,可以从.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"}
    });
}

© www.soinside.com 2019 - 2024. All rights reserved.