I'm trying to paginate data so I can get an infinite scroll for posts on my app. I have a cloud function called getPosts where there are multiple get functions a reads. Everything works fine with that. But when I try thinking of pagination using cloud functions, I run into a problem sending the last snapshot as a query parameter. This snapshot will be super long and an unexpected length, way more than 3000 characters. Here is my getPosts function:
exports.getPosts = (req, res) => {
const postId = req.query.postId;
if(postId != null){
db
.collection('posts')
.doc(postId)
.get()
.then((doc) => {
if(!doc.exists){
throw 'postNotFound';
}
else{
const voteCount = sumValues(doc.data().votingOptions);
let liked;
let votingOptionsDictionary;
return db.collection('votes')
.where('postId', '==', doc.id)
.where('userHandle', '==', req.user.userHandle)
.get()
.then((voteDoc) => {
return db.collection('likes')
.where('postId', '==', doc.id)
.where('userHandle', '==', req.user.userHandle)
.get()
.then((likeDoc) => {
liked = likeDoc.empty ? false : true;
return res.json([{
postId: doc.id,
userHandle: doc.data().userHandle,
postQuestion: doc.data().postQuestion,
userImageUrl: doc.data().userImageUrl,
imageUrl: doc.data().imageUrl,
postDescription: doc.data().postDescription,
createdAt: doc.data().createdAt
}]);
});
});
}
})
.catch((err) => {
if(err == "postNotFound"){
return res.json({'Error': `Post ID ${postId} does not exists`});
}
else{
console.error(err);
return res.json({error: err});
}
});
}
else{
db
.collection('posts')
.orderBy('createdAt', 'desc')
.limit(10)
.get()
.then(async (data) => {
const promises = await data.docs.map((doc) => {
const voteCount = sumValues(doc.data().votingOptions);
let liked;
let votingOptionsDictionary;
return db.collection('votes')
.where('postId', '==', doc.id)
.where('userHandle', '==', req.user.userHandle)
.get()
.then((voteDoc) => {
return db.collection('likes')
.where('postId', '==', doc.id)
.where('userHandle', '==', req.user.userHandle)
.get()
.then((likeDoc) => {
liked = likeDoc.empty ? false : true;
return {
postId: doc.id,
userHandle: doc.data().userHandle,
postQuestion: doc.data().postQuestion,
userImageUrl: doc.data().userImageUrl,
imageUrl: doc.data().imageUrl,
postDescription: doc.data().postDescription,
createdAt: doc.data().createdAt
};
});
});
})
Promise.all(promises)
.then((posts) => {
res.json(posts);
})
})
.catch((err) => {
console.error(err);
res.status(500).json({ error: err.code});
});
}
}
I was thinking of saving the snapshot object on the client side, then sending that as an optional query parameter to the getPosts cloud function to get the data I want, but I'm almost positive I can't send that object as a query paramter...
If you can't use an actual DocumentSnapshot object as the anchor document for your pagination, you can simply use the field values in the document that are relevant for sort ordering. This is described in the documentation. So, if you have a single ordering on createdAt, can can just pass the relevant field value to startAt() or startAfter().
If you don't have a order defined at all, then the sort order is based on the document ID, and you can simply use a where clause to get all documents greater than or less than the ID you specify. For example: where(FieldPath.documentId(), ">", id).
Related
I am a newbie in cloud functions. I want to create a new field counter whenever a document is created.
I tried the following codes:
exports.createCounter = functions.firestore.document('users/{userId}').onCreate((snap,
context) => {
console.log('onCreate created');
return snap.data.ref.set({counter: 0}, {merge: true})
.then(() => {
console.log("Count is created! "+userId);
})
.catch((error) => {
console.error("Counter Error writing document: ", error);
});
});
and using firebase-admin:
exports.createCounter = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
const id = context.params.userId;
console.log('onCreate created');
return admin.firestore().collection('users')
.doc(id).set({counter: 0}, {merge: true})
.then(() => {
console.log("Document successfully written! "+id);
})
.catch((error) => {
console.error("Error writing document: ", error);
});
});
But both are not triggered when a new document is created.
UPDATE
At first, I create users/userId/collection/doc. When it is created, I want to add a counter field into users/userId
Update following your comment
You can add a field to the users/userId document when you create a doc under the users/userId/theCollection collection by modifying the code of the second solution presented below.
Just trigger at the level of the subcollection, get the parent document id through the context object and build its DocumentReferencebased on this id, as follows:
exports.createCounter = functions.firestore.document('users/{userId}/theCollection/{docId}').onCreate((snap,
context) => {
const id = context.params.userId;
return admin.firestore().collection('users')
.doc(id).set({ counter: 0 }, { merge: true })
.then(() => {
console.log("Count is created! " + userId);
return null;
})
.catch((error) => {
console.error("Counter Error writing document: ", error);
return null;
});
});
Initial answer
The following should do the trick:
exports.createCounter = functions.firestore.document('users/{userId}').onCreate((snap,
context) => {
console.log('onCreate created');
const docRef = snap.ref;
return docRef.set({counter: 0}, {merge: true})
.then(() => {
console.log("Count is created! "+userId);
return null;
})
.catch((error) => {
console.error("Counter Error writing document: ", error);
return null;
});
});
Note that instead of doing snap.data.ref to get the DocumentReference you have to do snap.ref. As a matter of fact, snap is a DocumentSnapshot and you need to use its ref property.
Your second code snippet should normally work, but you need to return a value in the then() and in the catch() to indicate to the Cloud Function platform that the work of the function is complete. Note that you need to do the same thing for the above code. I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/ which explain this key point.
exports.createCounter = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
const id = context.params.userId;
console.log('onCreate created');
return admin.firestore().collection('users')
.doc(id).set({counter: 0}, {merge: true})
.then(() => {
console.log("Document successfully written! "+id);
return null;
})
.catch((error) => {
console.error("Error writing document: ", error);
return null;
});
});
Im trying to build a rest api, fetching a nested mysql queries.
When i fetch the first query, this return a array, then with this array i need to fetch data with another query for each value through a array.map
when the script running, always log a empty array, i think must be cause of promises. any help please?
//this the mysql queries
const getTournaments = 'SELECT ID FROM wp_posts WHERE post_type = "tournament"'
const getTournamentGame = 'SELECT meta_value FROM wp_postmeta WHERE meta_key = "tournament_game" AND post_id = ?'
async function fetchType(id){
return new Promise ((res, rej) => {
try{
pool.query(getTournamentGame, [id], (err, rows) => {
if (err) {
return rej(err)
}else {
return res(rows[0].meta_value)
}
})
} catch(err){
console.log(err)
}
})
}
async function mapeado(array) {
return new Promise (async (resolve,rej) => {
try{
var arr = []
array.map((item) => {
fetchType(item.ID).then((res) => {
var tourData = {
id: item.ID,
type: res
}
return tourData
}).then((data) => {
arr.push(data)
})
})
return resolve(arr)
} catch(err) {
console.log(err)
}
})
}
//making rest api
app.get('/tournaments', async (req, res) => {
pool.query(getTournaments, (err, rows) => {
mapeado(rows).then(console.log)
})
})
In the below code, when I remove the .where() from my query the query works and I get my comments returned. However, when I use the where query to try and find a comment bound to a post (scream) id, i get a blank array. Am I using .where() incorrectly? Any help would be appreciated.
exports.getScream = (req, res) => {
let screamData = {};
db.doc(`/screams/${req.params.screamId}`)
.get()
.then((doc) => {
screamData = doc.data();
screamData.screamId = doc.id;
return db
.collection('comments')
.where('screamId', '==', screamData.screamId) //query that doesnt seem to be working
.orderBy('createdAt', 'desc')
.get();
})
.then((data) => {
screamData.comments = [];
data.forEach((doc) => {
screamData.comments.push(doc.data());
});
return res.json(screamData);
})
.catch((err) => {
console.error(err);
res.status(500).json({ error: err.code });
});
}
Firestore document model
{
screamId: “id of post commenting on”,
body: “content of comment”,
userHandle: “username of comment”
}
I have a collection accounts structured as follow:
Now I have a user who owns two accounts:
How do I make a query to fetch the account of this user and return it as a resolution of a promise?
Here is what I tried. It returns []
getAccounts(user_id, userRef) {
return new Promise((res, rej) => {
this.db.runTransaction((transaction) => {
return transaction.get(userRef.doc(user_id)).then((userDoc) => {
let accounts = []
if (!userDoc.exists) {
throw "User Document does not exist!";
}
let userData = userDoc.data()
let accountList = userData.accounts
for (var id in accountList){
transaction.get(this.ref.doc(id)).then(ans => {
accounts.push(ans.data())
}).catch(e => {
console.log(e)
})
}
return accounts
}).then(function (ans) {
res(ans)
}).catch((e) => {
console.log(e)
});
}).catch(function (err) {
console.error(err);
rej(err)
});
})
}
You don't need to use a transaction, since you are just reading some documents. Since you want to execute, in parallel, two (or more) asynchronous methods which return a promise (i.e. the two get() for the accounts docs) you should use Promise.all().
Something along the following lines should work:
getAccounts(user_id, userRef) {
return db.collection('users').doc(user_id).get() //replaced since I am not sure what is this.db in your case
.then(userDoc => {
const promises = []
if (!userDoc.exists) {
throw "User Document does not exist!";
}
let userData = userDoc.data()
let accountList = userData.accounts
for (var id in accountList){
promises.push(db.collection('accounts').doc(id).get())
})
return Promise.all(promises)
})
.then((results) => {
return results.map(doc => doc.data());
})
.catch(err => {
....
});
}
Note that I have used the "classic" definition for the DocumentReferences (i.e. db.collection('users').doc(user_id) and db.collection('accounts').doc(id)) since I am not 100% sure what are this.ref and this.db in your case. Just adapt as you wish!
You may also rework it with return new Promise((res, rej) => {}) as you wish but the overall philosophy remains the same.
i'm replicating this code from the firebase documentation in google cloud functions:
var cityRef = db.collection('cities').doc('SF');
var transaction = db.runTransaction(t => {
return t.get(cityRef)
.then(doc => {
// Add one person to the city population
var newPopulation = doc.data().population + 1;
t.update(cityRef, { population: newPopulation });
});
}).then(result => {
console.log('Transaction success!');
}).catch(err => {
console.log('Transaction failure:', err);
});
But i get: property 'data' does not exist on type 'QuerySnapshot' when it should be a documentSnapshot.
I found the answer after i opened an issue on github and get some insight https://github.com/firebase/firebase-admin-node/issues/344
async function setCounts(storeRef: admin.firestore.DocumentReference) { // Type here
var transaction = admin.firestore().runTransaction(t => {
return t.get(storeRef)
.then((doc: admin.firestore.DocumentSnapshot) => { // Type here
x = doc.data(); // Now i can get data() from the doc
});
}).then(result => {
console.log('Transaction success!');
}).catch(error => {
console.log('Transaction failure:', error);
});
}
I finally made it work by explicitly declaring the types for DocumentReference and DocumentSnapshot, i don't know why, but while deploying the linter inferred doc as QuerySnapshot even though it was not.