Using send()
, end()
or redirect()
will terminate a HTTP Cloud Function:
Always end an HTTP function with send()
, redirect()
, or end()
. Otherwise, your function might to continue to run and be forcibly terminated by the system.
In your example, you are calling res.status(200).send(data.val());
inside your forEach
iteration of each child snapshot, therefore it will only get a chance to send the one response.
Likewise, because you have used a child_added
event listener, it is triggered once for every child at the specified path.
If you need to respond with all the query data at once, you'll be better off with a value
event listener instead, which will retrieve all data from your query in a single response:
exports.viewdata = functions.https.onRequest((req, res) => {
const userId = req.query.user;
admin.database().ref('users/' + userId)
.orderByKey()
.limitToLast(10)
.on('value', function(snapshot) {
res.status(200).send(snapshot.val());
});
});
However, if your intention is to build a response with each child separately, you could use res.write()
to write data to the response, and then eventually send this with end()
:
exports.viewdata = functions.https.onRequest((req, res) => {
const userId = req.query.user;
admin.database().ref('users/' + userId)
.orderByKey()
.limitToLast(10)
.on('value', function(snapshot) {
snapshot.forEach(function(data) {
res.write(data.val());
});
res.status(200).end();
});
});
Or, you could add them to a list before sending them all back as a response. The method you take here all depends on your end-goal though.
Unrelated to your initial question, but for completeness, please see the below observations and side notes, from comments:
The return
statement for admin.database().ref()
is not required for HTTPS triggers as they have a different lifecycle than other triggers and do not need to return promises.
If you have no need to listen for further changes after you've retrieved the necessary data, you should consider using once()
(to read data once) instead of on()
or removing the on()
listener with off()
.