To make your code work, just replace the following line :-
return collectionRef;
collectionRef.once('value').then(messagesData => {
with the following :-
return collectionRef.once('value').then(messagesData => {
The reason this works is that the return statement in return collectionRef
prevents further code from executing. Instead of that, you must return the promise(action) as is being done in my suggested replacement.
This should solve your problem right now, but as Frank van Puffelen mentioned in his comments, learning the concepts of promises would tremendously help you in the future!