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.
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?
OK, let´s see if I got this straight. You want:
- 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?
- 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.
Hi Giri, yes, I realized that my original question was not comprehensive enough. Here is the code that I have as of now. I used the example from https://www.wix.com/corvid/reference/$w.Dropdown.html#options
// Step 1: Set up dropdown list for Groups of which the current user is member of
wixData.query( ‘R1_USER_ASSIGNED_TO_GROUP’ )
.eq( “groupMember” ,userId)
.find()
.then ((results) => {
console.log( "20 Current User ID is " ,userId)
if (results.items.length === 0 ) {
$w( “#dropdown9” ).placeholder= “You have yet to be assigned to a group! Please wait…”
console.log( “23 No groups found for you” )
}
else
{ //for each match, find out group reference ID
for ( var i = 0 ; i < results.items.length; i++) {
var value = results.items[i];
let GRefID=value.groupName
console.log( “30 GRefID [” ,i, "] = " ,GRefID)
//For this GroupID, find the Group Name from PEER_GROUP collection
wixData.query( “PEER_GROUP” )
.eq( “_id” ,GRefID)
.find()
.then((PGQR)=>{
let PG=PGQR.items[ 0 ]
let PGName=PG.groupName
console.log( "38 PGName " ,PGName)
let PGLink= “PB JAM Active” +PGName+ “/discussion”
console.log( "40 PGLink " ,PGLink)
let NewOption={label:PGName,value:PGLink}
console.log( “42 NewOption is” ,NewOption)
// The next code line is generating the error “Error: The value passed to the element selector function (usually $w), must be of type string”
//NOTE: This is showing up only in published code when I open console, while in Preview console, I don’t get the error message! Just code doesn’t go beyond this line.
let PGOption=$w( “#dropdown9” ).options;
console.log( “44 Dropdown 9 options are” ,$w( “#dropdown9” .options))
//PGOption.push(NewOption)
PGOption.push({ “label” :PGName, “value” :PGLink})
console.log( "47 PGOption " ,PGOption)
$w( “#dropdown9” ).options=PGOption
console.log( “49 Dropdown 9 options are” ,$w( “#dropdown9” .options))
})
}
}})
What am I missing?
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.