I am currently working on a function to retrieve referral codes from users. The user inputs a code, which is then checked against the database to see if it exists or not
If the code provided matches the current user's code, it should not be accepted as self-referrals are not allowed
The code should match one of the codes stored in the database
However, I am facing an issue where even if the code exists in the database, the system fails to find a match. It works correctly when the referral code matches the current user's code and correctly points out that self-referrals are not permitted.
But in cases where the referral code matches another user's code, indicating a successful referral, the system still shows no match.
I need help in fixing this error
export const getID = functions.https.onCall(async(data, context) => {
const db = admin.firestore();
const usersSnapshot = await db.collection("user").get();
const allUIDs = usersSnapshot.docs.map(doc => doc.data().userID);
const userID = context.auth.uid;
const providedID = "cNx7IuY6rZlR9mYSfb1hY7ROFY2";
//db.collection("user").doc(providedID).collection("referrals").doc(userID);
await check();
function check() {
let result;
allUIDs.forEach(idFromDb => {
if (providedID === idFromDb && (idFromDb === userID)) {
result = "ownmatch";
} else if (providedID === idFromDb && (idFromDb !== userID)) {
result = "match";
} else {
result = "nomatch";
}
});
return result;
}
if (check() === "match") {
return {
message: `Match Found`,
};
} else if (check() === "ownmatch") {
return {
message: `Sorry, you can't use your own invite code`,
};
} else {
return {
message: `No User with that ID`
};
}
});