【发布时间】:2021-09-27 00:41:07
【问题描述】:
((已解决))
希望得到一些帮助来解决这个奇怪的问题,首先我是 JS 和 Firebase 的新手,所以我可能在这里错过了明显的东西。在任何情况下,我都在尝试将时间戳转换为负数,以便对文档进行排序,如果我在查询调用中限制它们,则始终获取最新的 10 个。
但是,下面的代码尝试在 Cloud Functions 中运行时会引发错误。
如果我 console.log (-Math.abs(timestamp)) 客户端但是,我得到一个负数就好了。有人在这里帮助解决这个问题和/或提供解决方法吗?
// create welcome post on new user signup
exports.createWelcomePost = functions.auth.user().onCreate((user) => {
const uid = user.uid;
const userName = user.displayName;
const photoURL = user.photoURL;
const timestamp = admin.firestore.FieldValue.serverTimestamp();
const order = -Math.abs(timestamp);
const db = admin.firestore();
const docRef = db.collection('posts');
docRef.add({
order: order,
created: timestamp,
createdBy: uid,
userName: displayName,
userImage: photoURL,
content: "...just joined the Startly Community, let's give him a warm welcome!",
featuredImage: "assets/img/welcome_post.jpg"
})
.then((docRef) => {
console.log("Welcome post created with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error creating post: ", error);
});
// ...
});
【问题讨论】:
-
您能否分享一下您想要获取最新 10 个的查询?您不需要将时间戳添加为负数。
标签: javascript node.js firebase google-cloud-firestore google-cloud-functions