I developed this firebase function to return data from my app search ( elastic ). When I log it at the functions console I can see the request data, and I can also see the response body at my App Search API Logs. But i can not get the data on the client
Response body
{
"meta": {
"alerts": [],
"warnings": [],
"precision": 2,
"page": {
"current": 1,
"total_pages": 1,
"total_results": 2,
"size": 10
},
"engine": {
"name": "engine",
"type": "default"
},
"request_id": "DF4VEuzuQpWAHH8VCBkvDg"
},
"results": [
{
"nome": {
"raw": "Produto legal"
},
"_meta": {
"engine": "engine",
"score": 0.8957006,
"id": "bnxvuzVXIQ4BxqHxjvgt"
},
"id": {
"raw": "bnxvuzVXIQ4BxqHxjvgt"
}
},
{
"nome": {
"raw": "Produto massa"
},
"_meta": {
"engine": "engine",
"score": 0.3164503,
"id": "rdG192xjSVCLIEBNg0la"
},
"id": {
"raw": "rdG192xjSVCLIEBNg0la"
}
}
]
}
Firebase function
exports.searchProdutos = functions.https.onCall((data, context) => {
const options = {
search_fields: { nome: {} },
result_fields: { nome: { raw: {} } },
};
const query = data.query;
client
.search("engine", `${query}`, options)
.then((response) => {
return response;
})
.catch((error) => console.log(error.errorMessages));
});
But when I try to call it in the client and log the response I get two null objects logged in the console
Function call on the client
const searchProdutos = httpsCallable(functions, "searchProdutos");
const getProdutos = async () => {
const results = await searchProdutos({ query: "produto" });
console.log(results.data);
};
getProdutos();
Aditional info: I am using Nodejs App Search in my functions and react as my frontend library/framework
Edit: I am also getting a 200 response. So I guess there is nothing wrong with the request. I tried removing the .then from my client function but I am still getting data: {null}
twice
CodePudding user response:
I am posting a Community Wiki answer for this thread in order to have a solution available for future users. Based on the comments from Kevin B in the chatroom thread, there is a problem with the client side code which calls your Firebase function. When your client code logs the response with a then
statement, this then
statement does not return the response, but an undefined
value, this makes the response get lost after being logged.
In addition, the server side Firebase function does not return the promise back to the client application inside the client.search()
function. Based on the chatroom comments, your Firebase function should return the response from client.search()
.