WriteBatch.commit error, could someone take a look? Thank you
Hi! So I'm trying to write a Node cloud function that uses axios to fetch one endpoint, then use the response to create several new requests to another endpoint using the first requests data.
Fetching of the apis work alright, but when I try to commit the batch I've created I get an error. I'm quite sure I've some silly mistake in my code as I'm a total beginner, so if someone more experienced could take a look I would be veeeery grateful!
Here is the whole index.ts
/* eslint-disable max-len */
import * as functions from "firebase-functions";
import axios from "axios";
const admin = require("firebase-admin");
const serviceAccount = require("xyz.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xyz.firebaseio.com",
});
const db = admin.firestore();
const ApiKey = "xyz";
exports.nyseQuotes = functions.region("europe-west3").pubsub.schedule("every 5 minutes").onRun((context: any) => {
/** Socket hang up if I dont delete these **/
delete process.env["http_proxy"];
delete process.env["HTTP_PROXY"];
delete process.env["https_proxy"];
delete process.env["HTTPS_PROXY"];
axios
.get("https://api.xyz.com/fetch1", {
params: {
apikey: ApiKey,
exchange: "NYSE",
},
})
.then((result: any) => {
const Element = result.data.data;
functions.logger.log("1. " + result.data.data);
return Element;
}).then((response: any) => {
functions.logger.log("2. " + response);
const Quotes = Promise.all(response.map((response: any) => {
const Batch = db.batch();
axios.get("https://api.xyz.com/fetch2", {
params: {
apikey: ApiKey,
symbol: response.symbol + ":NYSE",
interval: "1week",
},
}).then((response: any) => {
const Result = response.data;
functions.logger.log("3. " + Result.symbol + " " + Result.open);
const NewRef = db.collection("results").doc(`${Result.symbol}`);
const NewData = {name: Result.name, symbol: Result.symbol, open: Result.open, change: Result.change};
Batch.set(NewRef, NewData, {merge: true});
Quotes.then(() => {
return Batch.commit().then(function() {
functions.logger.log("commited the batch");
return "ok";
});
});
}).catch((err) => {
functions.logger.log("Axios error 2", err);
return "error";
});
}));
return response;
});
}
);
And here are the logs:
-NyseQyotes xyz Unhandled rejection
-NyseQyotes xyz Error: 4 DEADLINE_EXCEEDED: Deadline exceeded at Object.callErrorFromStatus'
-NyseQyotes xyz Caused by: Error
-NyseQyotes xyz at WriteBatch.commit
-NyseQyotes xyz Error: Process exited with code 16 at process.
Thank you for any insight! I really appreciate it :)
Posted Feb 19, 2021, 10:04 AM UTC · edited
24