Reputation: 27
I'm trying to get data from Firestore via Cloud Functions to Flutter and it doesn't seem to work. When I'm calling the Cloud Function through localhost it works fine so the problem is the connection between Flutter and Cloud Functions.
Flutter function:
Future<List> getMessages(from, to) async{
final HttpsCallable callable = _functions.getHttpsCallable(
functionName: 'getMessages',
);
dynamic resp = await callable.call(<String, dynamic>{
'friendDto': '{"from":"$from", "to": "$to"}',
});
return resp;
}
Cloud Function:
app.get("/users/getMessages/:friendDto", async function getUser(req: express.Request, res: express.Response) {
const friendDto = req.params.friendDto;
const parsedData = JSON.parse(friendDto);
const id = parsedData.from;
const number = parsedData.to;
const howLong = timer();
const messages = db.collection('messages');
const snapshot = await messages.where('from', '==' ,id).where("to","==",number).get();
console.log("This took: " + howLong.ms);
if (snapshot.empty) {
res.status(200).send('No matching documents.');
return;
}
const resArray: any[] = [];
snapshot.forEach((doc: { id: any; data: () => any; }) => {
resArray.push(doc.data());
});
res.status(200).send(resArray);
return resArray;
});
I'm pretty new to flutter and Ts so please don't be to harsh on my code ;)
Upvotes: 0
Views: 1701
Reputation: 4136
Node Js Backend Function
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.test = functions.https.onCall(async (data, context) => {
functions.logger.info("Hello logs: ", {structuredData: true});
functions.logger.info( data.token, {structuredData: true});
}
Flutter frontend
1- pubspec.yaml
cloud_functions: ^1.1.2
2 - Code
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('test');
final HttpsCallableResult results = await callable.call<Map>( {
'token': token,
});
Upvotes: 0
Reputation: 317828
You're mixing up callable functions and normal HTTP functions. On your client, you're expecting to invoke a callable function, but on your backend you have a normal HTTP function defined via an Express app. You can't use the callable function library on the client to invoke arbitrary backend functions. The backend function must also use the provided SDK as described in the documentation. Be sure to read that linked documentation carefully to understand how they work.
If you want to invoke your function as it stands now, you will need to abandon using the client SDK and instead using a normal HTTP request.
Upvotes: 1