Here is my code, but can't get it to work
//Find the group name for group id MyGroups01 wixData.query("PEER_GROUP") .eq("_id",MyGroups01) .find() .then((PGQR)=>{ let PGName01=PGQR.items[0] let PG_NAME_01=PGName01.groupName console.log("37 Group Name from Peer Groups is ",PG_NAME_01) }, console.log("39 Uh-oh, No Peer Group found based on ",MyGroups01) )
I only see the "39 Uh-oh.." message in console.
Here is my PEER_GROUP collection:
Now, as you can see, though the MyGroups01 value is same as the ID of the first item in PEER_GROUP, the promise is not resolved. What am I missing?
Think you are running into an unresolved promise. This code:
.then((PGQR)=>{ let PGName01=PGQR.items[0] let PG_NAME_01=PGName01.groupName console.log("37 Group Name from Peer Groups is ",PG_NAME_01) }, console.log("39 Uh-oh, No Peer Group found based on ",MyGroups01)
has the closing curly bracket BEFORE the second console.log. Therefor, promise is waiting to be resolved, in the mean time, code runs on and prints the second console.log.
Try this :
async function getPeerGroup(MyGroups01){
let PGQR= await wixData.query("PEER_GROUP")
.eq("_id",MyGroups01)
.find()
}
if(PGQR.items.length >0){
let PGName01=PGQR.items[0] let PG_NAME_01=PGName01.groupName
console.log("groupname=" + PG_NAME_01);
}
and call it with:
let objGroupName = await getPeerGroup("someid");
Just remember to make every function async when you do an await inside. This goes for the on body as for any other function. Example:
$w.onReady(async function () {
Didn´t test code, but it should be pretty close. Have fun.
OK, now the code seems to be working as shown above... only thing I changed was removing $w("#dropdown").options from console.log statement where error was occurring.
OK, let´s see if I got this straight. You want:
1) get a list of groups a member belongs to for use in a dropdown. In your example, pvstestuser03 would see 2 groups, the rest 1, OK?
2) user_id = group_id, OK? It´s because this is kind of different than what you tried to achieve first. There you took the first one only.
Thanks for quick response Giri! I added your code, but I think I am making some more mistakes.
Let me state what I am trying to achieve, so you can provide me better guidance:
I have users who login and are assigned to different user groups (PEER_GROUP)
On their profile page, I want to add a dropdown list which will be user specific
That is, a logged-in user will be able to see the active groups in the drop down menu
To this end, I have a collection for PEER_GROUP and a relationship collection R1_USER_ASSIGNED_TO_GROUP (see below)
Group Member and UserID are the same (which I use to find how many active groups a user belongs to, except right now, I have not added the date range constraint)
Active groups are those whose start date is less than or equal to today, and end date is greater than or equal to today
Typically, I expect users to have 3-4 active groups max
Now, if you look at my code, how should it be? I am currently using a brute force approach, but your code glimpse showed that a much more elegant coding exists. Would you please take a look?