'How can I write this function into my existing code, Keep getting a promise rejected error
Hi I am trying to Include the function below into my existing code so that after the users are filter based on matching city they will be filtered by the most matching to least matching number of objects. I wrote a function in replit but I am not sure how to translate it into the code I already have. Any pointers would be great.
This is my sorting function
function sortByMatch (loggedInuser) {
const users = new Set(loggedInuser)
const rank = arr => arr.filter(user => users.has(user)).length
return (a, b) => rank(b.users) - rank(a.users)
}
let loggedInuser = [
"sophia",
"Redlands",
"10", "10", "10", "10"
];
let matusers = [{users:
[
"Isai",
"Redlands",
"10", "8", "10", "8" ]
},
{ users:
[
"Ivan",
"Redlands",
"10", "10", "10", "10"]
//mon: 10, tues: 10, Weds: 10, thurs: 10
},
{ users:[
"David",
"Moreno Valley",
"10", "8", "10", "8"]
//mon: 10, tues: 10, Weds: 10, thurs: 10
},
{users:[
"Nathan",
"Redlands",
"10", "8", "10", "8"]
//mon: 10, tues: 8, Weds: 10, thurs: 8
},
{ users :[
"Kyle",
"Redlands",
"10", "8", "10", "8"]
//mon: 10, tues: 10, Weds: 10, thurs: 10
},
{ users : [
"Jack",
"Redlands",
"10", "10", "10", "10"
]}];
//const cuser = [];
//{ items: ['G', 'B', 'A'] },
// { items: ['C', 'B', 'A'] }
console.log()
const sorted = matusers.slice().sort(sortByMatch(loggedInuser))
console.log(sorted.map(user => user.users.join(', '))
This is the file I need to add my sorting function too after I filter the users by matching Address to current user.
const getUserProfile = uid => {
return firestore()
.collection('users')
.where('uid', '==', uid)
.get()
.then(querySnapshot => {
if (!querySnapshot.empty) {
const user = querySnapshot.docs[0].data();
return user;
}
});
};
const S_DriverList = () => {
const navigation = useNavigation();
const [loading, setLoading] = useState(true);
const [users, setUsers] = useState([]);
const [address, setAddress] = useState('');
useEffect(() => {
const subscriber = firestore()
.collection('users')
.onSnapshot(querySnapshot => {
const FBusers = [];
querySnapshot.forEach(documentSnapshot => {
FBusers.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
auth().onAuthStateChanged(user => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
const profile = getUserProfile(user.uid);
profile.then(pro => {
// const LGusers = pro;
const addy = pro.Address;
setAddress(pro.Address);
setUsers(pro);
const filterdUsers = FBusers
.filter(user => {
return user.Address === addy && user.uid !== pro.uid;
})
});
// ...
} else {
console.log('no user :(');
// User is signed out
// ...
}
});
setLoading(false);
});
return () => subscriber();
}, []);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
