Reputation: 505
I've read all the related questions and watched the Firebase videos re Promises and I still can't get rid of the above error. The attached code runs fine locally, yet when done from cloud functions I keep on getting this error. Any help would appreciated!
exports.enquiry = functions.firestore.document('users/{userId}/enquiries/{enquiryId}')
.onCreate((snap, context) => {
let enquiryDoc = snap.data();
let {
uid,
type
} = enquiryDoc;
function ResponseGeneration(type) {
switch (type) {
case 'type1':
{
Response = 5;
return Promise.resolve(Response);
}
case 'type2':
{
var body = `<QUOTA><DATE>20181203</DATE></QUOTA>`;
const call = request.post({
url: "xxx",
port: 443,
method: "POST",
headers: {
'Content-Type': 'text/xml',
},
body: body,
json: false
})
.then((parsedBody) => {
console.log(parsedBody.statusCode);
console.log(parsedBody);
return xml2js(parsedBody);
})
.then((js) => {
console.log('JSON.stringify(js):', JSON.stringify(js));
var json = JSON.stringify(js);
var jsonParsed = JSON.parse(json);
if (jsonParsed.hasOwnProperty('Error')) {
Response = jsonParsed['Error']['Message'];
} else {
Response = jsonParsed['RESULT'];
}
console.log("Response: ", Response);
return Promise.resolve(Response);
}).catch(function(err) {
console.log("error from post:", err);
// return Promise.resolve(err);
});
return Promise.all(call); //also tried with Promise.resolve
}
default:
return Promise.resolve(0.0);
}
}
ResponseGeneration(type).then((response) => {
return snap.ref.update({
quote: response,
status: 'quoted',
quoteId: context.params.enquiryId
});
});
});
As you can see, it returns Promises everywhere, yet Firestore console keeps printing:
Function returned undefined, expected Promise or value
I'm not quite seeing where else it might be missing a promise. Any help?
Upvotes: 1
Views: 163
Reputation: 5613
Your top level function called by onCreate()
isn't returning anything, thus the error.
You need to return the Promise
from your call to ResponseGeneration
:
return ResponseGeneration(type).then((response) => {
return snap.ref.update({
quote: response,
status: 'quoted',
quoteId: context.params.enquiryId
});
});
Upvotes: 1