Ad

Firebase Cloud Function Return Json Object From Batch Function

I would like to return to the client Json object {mesage: "this is what I want to return"} from batch operation... What am I missing here?

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

I case where snapshot is undefined I would like to return myFunction() and not return { error: "undefined" }

Ad

Answer

Figured out that I can't call batch in the middle of the transaction I can call it from .then((data) block. So what I did was:

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

Ad
source: stackoverflow.com
Ad