I use this function to wait for an HTTP request from my app. Once it gets fired, it will use its filter function to only add items to the array that match the search term given by the http request.
Can it be optimized for speed performance?
exports.getFoodnames = functions.https.onRequest((request, response) => {
cors(request, response, () => {
const ref = admin.database().ref('foodnames');
ref.once('value').then((snapshot)=> {
let filtered_name = [];
let searchTerm = request.query.search;
snapshot.forEach(snap => {
if (snap.val().name.indexOf(searchTerm) !== -1) filtered_names.push({ name : snap.val().name, $key: snap.key});
});
response.status(200).send(JSON.stringify(filtered_names));
});
});
});