Here is my issue, I have a callable Cloud Function I would like to use on my Flutter app.
You can find the code below.
However, each time I press the button corresponding to the function I always have a “null” value on my app coming from Cloud Functions even if my answer is printed into the console.
From what I understand, the function return a “null” value as the jsonResponse is not here yet.
I would like to find a way to return the jsonResponse only when the answer is there.
I tried async, await, and promises already, but maybe I used it the wrong way ?
Thank you
exports.writeMessage = functions.https.onCall(async (data, context) => {
global.XMLHttpRequest = require("xhr2");
function makeRequest(method, url) {
appleMusicKey = 'XXXXXXX'
return new Promise(function (resolve, reject) {
let http = new XMLHttpRequest();
http.open(method, url);
http.setRequestHeader('Authorization', 'Bearer ' + appleMusicKey);
http.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(JSON.parse(http.response)['data'][0]['attributes']['url'].toString());
} else {
reject({
status: this.status,
statusText: http.statusText
});
}
};
http.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
http.send();
});
}
async function doAjaxThings() {
var isrcCode = 'QMBZ92168114';
let result = await makeRequest("GET", `https://api.music.apple.com/v1/catalog/fr/songs?filter[isrc]=${isrcCode}`);
console.log(result);
return new Promise(function (resolve, reject) {
resolve(result);
reject('error');
});
}
doAjaxThings();
});