The function below was previously triggered by onCreate in firestore but I will prefer it as an (new) onCall. I have tested variations of it and its worked 20% of the time. From what I've read online this means I'm not handling my returns and promises correctly. If anyone can tell me what I'm doing wrong or give me tips, it will be greatly appreciated.
export const DataStoreCall = functions.https.onCall((datas) => {
const filePath = 'goodfile';
const tempLocalFile = path.join(os.tmpdir(), filePath);
var file = fs.createWriteStream(tempLocalFile);
const cors = require('cors')({ origin: true });
const newV = datas;
const db = admin.firestore();
const Id = datas.id;
const UpstRef = db.collection('Review').doc(Id)
https.get(newV.url, function (response) {
return async function () {
try {
response.pipe(file);
const colPath = 'dataStuff';
const a = file.path;
const b = newV.name
const colRef = db.collection(colPath)
const batch = db.batch();
let data;
if (b.endsWith(".json")) {
data = await fs.readJSON(a);
}
else if (b.endsWith(".csv")) {
data = await parseCSV(a);
}
else if (b.endsWith(".xlsx")) {
data = await parseXLSX(a);
}
else if (b.endsWith(".xlx")) {
data = await parseXLSX(a);
}
else {
throw "Unknown file extension.";
}
for (const item of data) {
const docRef = colRef.doc();
batch.set(docRef, item);
}
// Commit the batch
await batch.commit().then(() => {
//FIRESTORE Update
})
console.log("completed!");
} catch (error) {
console.log("failed!", error);
}
}
function parseCSV(path): Promise<any> {
return new Promise((resolve, reject) => {
let lineCount = 0;
///CSV read/parse code
resolve(data);
.on("error", err => reject(err));
}
function parseXLSX(path): Promise<any> {
return new Promise((resolve, reject) => {
//XLSX read/parse code
resolve(data);
})
}
})
I have the blaze plan and have enabled cors/http request on the storage
parseXLSXandparseCSVfunctions, based on logs. I am not getting any errors in the logs. The function is just not completing. Aside from the HTTP return, is there anything else that needs to be changed? Been working on it for 2 days now.