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
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad