I´m trying to compare my current uid with uid from one collection, but im failing could someone please help me?
This is my Code:
const user1 = auth.currentUser.uid;
const [users, setUsers] = useState([]);
useEffect(() => {
const usersRef = collection(db, "Admin");
const q = query(usersRef);
const unsub = onSnapshot(q, querySnapshot => {
let users = [];
querySnapshot.forEach((doc) => {
users.push(doc.data());
});
setUsers(users);
});
return () => unsub();
})
console.log(users);
if(user1 === users.uid){}
CodePudding user response:
The code is showing a list of many users
. But a list does not have an id
attribute.
The code is trying to compare a list of users to a single user.
Thus, users.uid
will not work.
Only a single user
object might have an id
.
Query for a single user and compare its id
along these lines: user1 === user.uid
.
CodePudding user response:
In your case here, users
is an array so you can't write:
if(user1 === users.uid){}
Instead you need to to find if user1
exists in the users
array first and then do the check like this:
const user1Found = users.find(user => user1 === user.uid);
if (user1Found) {}