Unable to return an array of json from Cloud Firestore via Cloud Functions (onCall) to Swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Unable to return an array of json from Cloud Firestore via Cloud Functions (onCall) to Swift



I have a problem getting the result from a Cloud Function.



This is my Cloud Function:


exports.retrieveTrips = functions.https.onCall((data, context) => {
const uidNumber = context.auth.uid;

var arrayOfResults = new Array();

var idOfFoundDoc;
var query = admin.firestore().collection('Users').where('UID','==', uidNumber);
query.get().then(snapshot =>

snapshot.forEach(documentSnapshot =>

idOfFoundDoc = documentSnapshot.id;
);
var queryDoc = admin.firestore().collection('Users').doc(idOfFoundDoc).collection('Trips');
queryDoc.get().then(snapshot =>

snapshot.forEach(documentSnapshot =>

arrayOfResults.push(documentSnapshot.data());
);
console.log('ARRAY: ' , arrayOfResults);
return arrayOfResults;
)
.catch (err =>

console.log ('Error adding document: ', err);
);

)
.catch (err =>

//response.send('Error getting documents', err);
console.log ('Error getting documents', err);
);



And this is the code that I have in my application.


@IBAction func RetrieveTripsButton(_ sender: Any)


self.functions.httpsCallable("retrieveTrips").call() (result, error) in
if let error = error as NSError?
if error.domain == FunctionsErrorDomain

let message = error.localizedDescription
print ("Message: " + message)

return


print ("Result: -> (type(of: result))")
print("Result.data type: (type(of: result?.data))");
print ("Result.data -> (result?.data)")




And this is the printed result.


Result: -> Optional<FIRHTTPSCallableResult>
Result.data type: Optional<Any>
Result.data -> Optional(<null>)



The console log is able to print arrayOfResults correctly. Furthermore, when I change this functions to onRequest and feed it the relevant information, the res.status(200).send(arrayOfResults) is able to display the array of JSON in the page.


res.status(200).send(arrayOfResults)



If I placed the return arrayOfResults; outside of the .then function, I would get a result along with an empty array. My issue is similar to this problem here but I'm unable to receive even that when I return text: "some_data" ; .


return arrayOfResults;


.then


text: "some_data" ;



Any help would be great, thank you!




1 Answer
1



You have to chain the different promises and return the result of the promises chain, as follows.



Note that it is actually what the OP explains in his answer to the SO post you mention "The issue was that I forgot to return the actual promise from the cloud function".


exports.retrieveTrips = functions.https.onCall((data, context) =>
const uidNumber = context.auth.uid;

const arrayOfResults = new Array();

let idOfFoundDoc;
const query = admin.firestore().collection('Users').where('UID','==', uidNumber);

return query.get().then(snapshot => // here add return
snapshot.forEach(documentSnapshot =>

idOfFoundDoc = documentSnapshot.id;
);
const queryDoc = admin.firestore().collection('Users').doc(idOfFoundDoc).collection('Trips');
return queryDoc.get(); // here add return and chain with then()
)
.then(snapshot =>
snapshot.forEach(documentSnapshot =>
arrayOfResults.push(documentSnapshot.data());
);
console.log('ARRAY: ' , arrayOfResults);
return arrayOfResults : arrayOfResults ; //return an object

)
.catch (err =>
console.log ('Error getting documents', err);
//Here you may return an error as per the documentation https://firebase.google.com/docs/functions/callable#handle_errors, i.e. by throwing an instance of functions.https.HttpsError
);

);



I would also suggest that you look at these two videos from the Firebase team, about Cloud Functions and promises: https://www.youtube.com/watch?v=7IkUgCLr5oA and https://www.youtube.com/watch?v=652XeeKNHSk.





This worked out great, thank you very much for the help and the links, I will check them out.
– WyVouse
Aug 7 at 8:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS