(( SOLVED ))
Looking to get some help to figure out this weirdness, first up I'm new to JS & Firebase, so I might be missing the obvious here. In any case, I'm trying to turn the timestamp into a negative number, to order documents and always get the latest 10 if I limit them in a query call.
However, the code below throws an error when trying to run it in Cloud Functions.
IF I console.log the (-Math.abs(timestamp)) client-side however, I get a negative number just fine. Anyone here to help with this problem and/or provide a workaround?
// 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);
});
// ...
});