I have a firestore-collection "Messages
" with a boolean-field "viewed
" and a field "userToRef
" which contains a coll:Users
-reference. I want my cloud-function to update the field "viewed
" to "True
" in all docs in the coll:Message
, that have the same user-reference in the field "userToRef
" as the URL-parameter "userRef
".
But whatever I do, it envokes the 404-error
:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.allMsgOfUserRead = functions.https.onRequest((req, res) => {
// Get the user reference from the request with the id of the document
const strRef = req.query.userRef;
const userRef = admin.firestore().collection('Users').doc(strRef);
// Update the "viewed" field of all documents in the "Messages" collection
// where the "userToRef" field matches the user reference
return admin.firestore()
.collection('Messages')
.where('userToRef', '==', userRef)
.get()
.then(snapshot => {
if (!snapshot.exists) {
// Document does not exist, return an error response
return res.status(404).json({
message: `Messages to User not found ${req.query.userRef}`,
code: 404
});
}
snapshot.forEach(doc => {
doc.ref.update({ viewed: true });
});
return res.json({
message: 'Success',
code: 200
});
})
.catch(error => {
return res.status(500).json({
message: 'Error occurred',
code: 500
});
});
});
I really would need an idea, why that happens…Thanks!
2
Answers
I have never tried to write the code like this, but I strongly doubt what you will get when you use
admin.firestore().collection('Users').doc(strRef)
as a key in the search code.where('userToRef', '==', userRef)
.Maybe what you want to use is
req.query.userRef
instead?You should not execute a set of calls to the asynchronous
update()
method in aforEach()
loop. You should usePromise.all()
as shown below. Also, note that aQuerySnapshot
always exists and might be empty: use theempty
property to check that.Since there is an
if
block in your code, I would use theasync
/await
keywords in order to have a simpler and cleaner code:Final remark: The
QuerySnapshot
most probably contains 0 or 1QueryDocumentSnapshot
(only one user correspond to a specific User Reference, isn’t it?), so you don’t really need a loop. If the TheQuerySnapshot
is not empty , you can get the unique doc withsnapshot.docs[0]
.