Javascript .sort by time not working on array? - javascript

I have a flatlist which displays all the posts of users that the current user is following. This is normal in instagram, twitter, all social networks etc.
I want to display them chronologically, but it is not working.
The code works like this:
I query Firestore for the current user's posts
I have a list of UID's of ever user the current user is following
I query Firestore for the posts of whoever the current user is following
This returns all the posts I want
The posts are in blocks. Ex, current user's posts are added to the array. Then user1 current user is following's posts get added. Then user2's posts get added. Etc.
I attempt to run a .sort function provided by Javascript to order the posts chronologically
Here is the code for it (removed the doc fields as they are not important, except date_created):
getCollection = async (querySnapshot) => {
const followingPosts = [];
await Firebase.firestore() <----------------------- Get current users posts
.collection('globalPosts')
.where("uid", "==", Firebase.auth().currentUser.uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
....other fields
date_created
....other fields
} = doc.data();
followingPosts.push({
....other fields
date_created
....other fields
});
})
});
querySnapshot.forEach(async (res) => {
await Firebase.firestore() <-------------- get following users posts, uid after uid
.collection('globalPosts')
.where("uid", "==", res.data().uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
....
date_created
....
} = doc.data();
followingPosts.push({
....other fields
date_created
....other fields
});
})
});
});
followingPosts.sort(function(a,b){ <-------- How I try to sort the posts by date created
return a.date_created.toDate() - b.date_created.toDate()
})
this.setState({
followingPosts,
isLoading: false,
});
}
Few notes:
The posts are fetching correctly (only the people that current user is following's posts show up)
The reason I am doing date_created.toDate() is because firestore timestamp objects are in nanoseconds and milliseconds. Whether I have date_created.toDate() or just date_created, it doesn't work.
I am aware that I can query firestore and order by date_created, descending in the query. But since the posts are being queried sequentially, this only orders the individual blocks of posts, not the entire array
I have tried putting the followerPosts.sort function INSIDE the query snapshot, after the for each. Not working either:
querySnapshot.forEach(async (res) => {
await Firebase.firestore()
.collection('globalPosts')
.where("uid", "==", res.data().uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
....other fields
date_created
....other fields
} = doc.data();
followingPosts.push({
....other fields
date_created
....other fields
});
})
});
followingPosts.sort(function(a,b){
return a.date_created.toDate() - b.date_created.toDate()
})
});
EDIT: more information on date_created:
Upon creation (Adding a new post to firestore), date_created is initialized like this:
date_created: new Date()
Within firestore, the above method of initializing date created looks like this:
When I console log the date_created, I am returned a firestore timestamp object:
t {
"nanoseconds": 14000000,
"seconds": 1610413574,
}
This is unusable for my purposes, so I convert this timestamp object using .toDate() when I pass the data to the flatlist:
<FeedCellClass
... other fields
date_created={item.date_created.toDate()}
/>
.toDate() converts it to this, which I can use for my purposes:
2021-01-12T01:06:14.014Z
Let me know how to solve this issue.

I solved my problem - I was sorting in the wrong place. Here is the solution:
querySnapshot.forEach(async (res) => {
await Firebase.firestore()
.collection('globalPosts')
.where("uid", "==", res.data().uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
...
date_created
} = doc.data();
followingPosts.push({
...
date_created
});
}) <----------- I put it in the on snapshot, instead of after.
followingPosts.sort(function(a,b){
return b.date_created.toDate() - a.date_created.toDate()
})
});
});
Thanks everyone

Related

How to get nested key in firebase real time database

I am creating a database for the products. The products are pushed into the database successfully but I am facing a problem in getting the data because the keys are nested. How can I target the nested key?
Please help me.
Using the below code I am getting my all the products data of currently logged in user.
useEffect(() => {
const getProductsData = async () => {
const userId = await AsyncStorage.getItem('uid')
database()
.ref(`Products/${userId}`)
.on('value', data => {
console.log(data.val())
});
}
getProductsData()
}, [])
console of the data.val()
Assuming that your userId is the dUD7M... value in the screenshot you shared, the data snapshot you get will contain the data for both child nodes in that screenshot. You can loop over those children with:
const userId = await AsyncStorage.getItem('uid')
database()
.ref(`Products/${userId}`)
.on('value', snapshot => {
snapshot.forEach((data) => { // 👈
console.log(data.val())
});
});

Get the var value out of IF statement

I'm trying to do a comparison between user session email and an email in an array in firestore. That is, I want to search for the login email within the database and if any email is found, bring some information to the screen, such as name and surname.
I even managed to get inside the array and make this comparison, but I can't make the "var UserName" leave the { } of the IF
Can someone help me?
my code is:
const [data, setData] = useState([]);
const getUsers = () => {
firestore()
.collection("users")
.get()
.then((querySnapshot) => {
let userFirestore = [];
querySnapshot.forEach((doc) => {
const usuario = {
id: doc.id,
nome: doc.data().nome,
sobrenome: doc.data().sobrenome,
email: doc.data().email,
profissao: doc.data().profissao,
}
userFirestore.push(usuario);
});
userFirestore.forEach(function (item, indice, array) {
if (item.email === user.email){ //user.email brings the email of the logged in user
var nomeUsuario = item.nome
console.log(nomeUsuario) //UserName brings the result I expected
}
});
})
.catch((error) => console.log(error));
}
You can use a query to get a document with current user's email that instead of fetching the whole collection as shown below:
firestore()
.collection("users")
.where("email", "==", user.email)
.get().then((querySnapshot) => {
if (querySnapshot.empty) {
console.log("User not found")
return;
}
const user = querySnapshot.docs[0].data();
console.log(user)
})
Even better, if you use user's UID as the Firestore document ID (highly recommended), then you can get that single document by ID as shown below:
firestore()
.collection("users")
.doc(user.uid) // < user.uid must be defined
.get()
.then((snapshot) => {
console.log(snapshot.data())
})
When you fetch the whole collection, you are charged N read where N is total number of documents in the collection and it'll be slower as you are downloading plenty of data.

How do I store user's activity in firestore?

I am building a social media app (very simple) an I want to store user's activity in firestore database. I have a collection of "users" and I keep user's id, user's username, user's profile pic there. But I dont think that user's activity should be stored there as well (correct me if I am wrong?)
So I created a new collection called UserActivity where I store user's activity. I wanted to store if a user has been searching on a profile so I do the following:
const logUserSearch = async (term) => {
await firebase
.firestore()
.collection("userActivity")
.doc(firebase.auth().currentUser.uid)
.collection("userSearch")
.add({
term: term,
date: firebase.firestore.FieldValue.serverTimestamp(),
})
};
I think the above query solves the problem with user's search term's. However I want to store if a user has visited a profile. So here is my question: what is the correct way to store if a user visited a profile? Should I add new subcollection "profileVisit", something like that:
const logProfileVisit = async (searchTerm, profileId) => {
await firebase
.firestore()
.collection("userActivity")
.doc(firebase.auth().currentUser.uid)
.collection("profileVisit")
.doc(profileId)
.add({
source: searchTerm,
date: firebase.firestore.FieldValue.serverTimestamp(),
})
};
But then how do I calculate which are the most "popular" profiles? Should I create my database in another way, like this:
const logProfileVisit = async (searchTerm, profileId) => {
await firebase
.firestore()
.collection("userActivity")
.doc(profileId)
.collection("profileVisit")
.add({
user: firebase.auth().currentUser.uid
source: searchTerm,
date: firebase.firestore.FieldValue.serverTimestamp(),
})
};
So that I can easily calculate which are the most "popular" profiles? What about the user case where I need to calculate "top 10 fan profiles" or something similar? I.e. How do I calculate who visited your profile most often?
A root level collection "userActivity" (or a sub-collection) should be enough. You can store the activity type as a field instead of sub-collections as shown below:
users -> {userId} -> userActivity -> {logId}
(col) (doc) (col) (doc)
But then how do I calculate which are the most "popular" profiles?
You can store a number field in that profile's document and whenever the logProfileVisit is called, increment that:
const usersCol = firebase.firestore().collection("users")
const logProfileVisit = async (searchTerm, profileId) => {
await Promise.all([
usersCol
.doc(currentUserId)
.collection("userActivity")
.add({
source: searchTerm,
date: firebase.firestore.FieldValue.serverTimestamp(),
type: "profileVisit"
}),
usersCol
.doc(profileUserId)
.update({
profileViews: firebase.firestore.FieldValue.increment(1),
})
])
};
You can also use batch writes while updating these fields so either both the operations fail or pass.
You can also use firestore -> audit logs -> pub/sub sink -> cloud function -> firestore.
I explain it a little more at https://justin.poehnelt.com/posts/tracking-firestore-user-activity/. Below is the function that listens to the Pub/Sub sink and writes back to Firestore.
import * as firebaseAdmin from "firebase-admin";
import * as functions from "firebase-functions";
export default functions.pubsub
.topic("firestore-activity")
.onPublish(async (message) => {
const { data } = message;
const { timestamp, protoPayload } = JSON.parse(
Buffer.from(data, "base64").toString()
);
const uid =
protoPayload.authenticationInfo.thirdPartyPrincipal.payload.user_id;
const writes = protoPayload.request.writes;
const activityRef = firebaseAdmin
.firestore()
.collection("users")
.doc(uid)
.collection("activity");
await Promise.all(
// eslint-disable-next-line #typescript-eslint/no-explicit-any
writes.map((write: any) => {
activityRef.add({ write, timestamp });
})
);
});
I then have a collection that looks like the following.

Firebase Collection Group Query in VueJS

created() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.userID = user.uid;
console.log(this.userID);
} else {
console.log('User is not logged in.');
this.$router.push('/login');
}
});
},
data() {
return {
pantry: [],
userID: '',
};
},
methods: {
getCategoryDataFromFirebase() {
const db = firebase.firestore();
db.collectionGroup("pantry")
.where("UserID", "==", this.userID)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
this.pantry.push(doc.data());
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
},
},
};
I am attempting to do a Collection Group Query where I grab all of the listings in my firestore database that are associated with the user ID that created them. I am able to get the user ID on the created hook and store it in my this.userID. I want to then use that ID on my getCategoryDataFromFirebase function to do the collection group query.
It's recommended to create the collection group query and get the SDK error back from firebase in the console, then use the attached link in the console to create the appropriate rules automatically instead of manually which I did. Now I am thinking that I must not be referencing the group correctly because I am unable to get any data back from my firestore. I have tried to create the group by the main "pantry" but I am thinking that I would possible need to drill down further into the database or to set up the query another way. I would appreciate any guidance that could be given. I have attached the view of my firestore as well for reference. I am attempting to get all of the userIDs in each category i.e. fruits, vegetables, etc.
When you use db.collectionGroup("pantry") you are reading from all collections named pantry. In your code that is only a single top-level collections.
If you want to read from all Vegetables collections, you need to query db.collectionGroup("Vegetables").

Cannot fetch list of UIDs inside "users" collection in firebase using reactjs

constructor(props) {
super(props);
this.state = {
users:[]
};
}
//method to get the data from users collection
async componentDidMount() {
const db = firebase.firestore();
db.collection("users")
.get()
.then(querySnapshot => {
const data = querySnapshot.docs.map(doc => doc.data());
console.log(data);
this.setState({ users: data });
})
.catch( err =>{
console.log(err);
});
}
this function returning an empty error
i want to print list of users uid
here i have users collection and inside it i have retailers collection and its document
One thing you have to realize here is that your users collection contains no documents. The document IDs are shown in italics, which means that there is no document here. The IDs are visible because there is a nested subcollection under each document. They are shown like this in the console so that you can click through and navigate to the nested subcollection, despite the document being missing.
If you want to list users with a query, you will have to actually create documents in the users collection. They can be empty if you don't have any information. But you do need actual documents in the users collection in order for anything to show up in a query.
This line in your current code querySnapshot.docs.map(doc => doc.data()) takes the data of each document. But you're keeping the UID in the ID of each document, so you'll want to use:
const db = firebase.firestore();
db.collection("users")
.get()
.then(querySnapshot => {
const data = querySnapshot.docs.map(doc => doc.id);
console.log(data);
this.setState({ users: data });
})
.catch( err =>{
console.log(err);
});
Update: As Doug pointed out in his answer, if there are no actual user documents, your get() call will not return it.
I highly recommend creating user documents, even if it's just an empty one.
For now, the only way to get the UID would be to load all retailers for all users, and then use those results to get the ID of the parent documents:
const db = firebase.firestore();
db.collectiongroup("retailers")
.get()
.then(querySnapshot => {
querySnapshot.forEach((doc) => {
console.log("retailed "+doc.id+" for user "+doc.ref.parent.parent.id);
});
})
.catch( err =>{
console.log(err);
});
You'll have to deduplicate the UIDs, but that will leads to getting the UIDs.
But you'll be loading all retailers for all users this way, so as said, I highly recommend storing a small/empty user document instead.
The data method on querySnapshot.docs gets the data now to get the id of each document you need to add access the id property as UID is stored in id property
async componentDidMount() {
const db = firebase.firestore();
db.collection("users")
.get()
.then(querySnapshot => {
const data = querySnapshot.docs.map(doc => doc.data().id);
console.log(data);
this.setState({ users: data });
})
.catch( err =>{
console.log(err);
});
}

Categories