I am creating an angular-based e-commerce app and this is the API call for checking the product shipment info. But this function is returning a null value when called.
Here is the code of my function:
exports.checkOrderShipment = functions.https.onCall(
(req: any, context: any) => {
if (!context.auth)
return { status: 'error', code: 401, message: 'Not signed in' };
// console.log('checkOrderShipment', req);
// console.log('checkOrderShipment CORS METHOD');
let authOptions = {
method: 'POST',
url: 'https://apiv2.shiprocket.in/v1/external/auth/login',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: 'test',
password: 'test',
}),
};
var a = new Promise(function (resolve, reject) {
request(authOptions, function (error: any, authResponse: any, body: any) {
if (error) return reject(error);
try {
resolve(JSON.parse(body));
} catch (error) {
reject(error);
}
});
});
return a.then(function (data: any) {
// console.log('authApi', data, typeof data);
// console.log(data);
let options = {
method: 'GET',
url:
'https://apiv2.shiprocket.in/v1/external/courier/track/shipment/' +
req.shipmentId,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + data.token,
},
};
var shipmentData = new Promise(function (resolve, reject) {
request(options, function (error: any, response: any) {
if (error) return reject(error);
// console.log('checkOrderShipment API RESPONSE', response);
resolve({data: {
res: response,
req: req,
body: response.body,
},});
});
});
shipmentData.then(function (data: any) {
console.log('shipmentData', data);
return data;
});
});
}
);
Here is my calling method:
My services file :
checkShipmentDetail(shipmentId){
return this.firebaseFunction.httpsCallable('checkOrderShipment')(shipmentId);
// return this.http.post(environment.cloudFunctions.checkOrderShipment, {shipmentId:shipmentId});
}
My component typescript page function:
this.paymentService
.checkShipmentDetail(shipmentID)
.subscribe((res: any) => {
console.log('check',res)})
I am new to the onCall
function and know nothing about how promises work in firebase functions. Now I knwo how to use on the Request method. Actually, I was using it previously but because of so many security problems and App Check I am moving to onCall
method for functions
.
Helpful link I found:
Node.js promise request return
And if possible also tell how to only allow authorized people to access the cloud functions.