【发布时间】:2020-09-03 02:22:12
【问题描述】:
我正在尝试对数据进行分页,以便可以无限滚动查看我的应用上的帖子。我有一个名为 getPosts 的云函数,其中有多个读取函数。一切正常。但是当我尝试使用云函数考虑分页时,我遇到了将最后一个快照作为查询参数发送的问题。此快照将超长且出乎意料的长度,超过 3000 个字符。这是我的 getPosts 函数:
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});
});
}
}
我正在考虑将快照对象保存在客户端,然后将其作为可选查询参数发送到 getPosts 云函数以获取我想要的数据,但我几乎可以肯定我无法将该对象发送为一个查询参数...
【问题讨论】:
标签: node.js google-cloud-firestore pagination google-cloud-functions