Reference problem

I have two collections (why don’t you call them tables?) “forums” and “threads”.
This is a one-to-many … many threads can contain a reference to the same forum.
I have some code here … the part I’m unsure on, is in bold.


forumIDString = wixLocation.url.substr(wixLocation.url.indexOf("/viewforum/") + 11); 

// find the Title of the given forum 
const forums = await wixData.query("forums").eq("_id", forumIDString) 
	.find(); 
var forumTitle = forums.items[0].title; // forum titles are unique 

// find threads with the given forum title 
const threads = await wixData.query("threads").eq( **"forumId", forums.items[0]** ) 
	.find(); 

This is not working, but I’m really not sure how to handle the reference in code.

When I used to program dB years ago, the references were all text values. It seems that with wix, they are actual items in the other collection.

Please help.

Bump. I’m sure this is not a hard question for anyone who knows how to do reference queries. Wiki Staff please?

Well, until someone can show me how to do this, I have solved it with another hack, involving some duplication of data.

I’m changing the reference field to a text field which has a copy of the unique field I want to refer to in the other table. I can just copy the text over when I create the record that needs the “reference”.

Hi Mike, try this:

const threads = await wixData.query("threads")
   //take out the _id value of the first item in the array 
   //and use it to find the threads that belong to that forum
  .eq("forumId", forums.items[0]._id)
  .find();

note that in as far as what is stored in a collection for a reference field, we only store the _id of the target object.
so you need to use that _id value in your query filter.

hope this helps and let me know if you need more info on how references work in wixcode.

I want to put a ‘Ratings’ in the ‘Product Page’, but it is showing the same Rating for all the products.
Can anyone help me in fixing this issue.
Thank you .

I tried the followings -

CODE -

import wixData from 'wix-data';
import wixWindow from 'wix-window';

export function ratingsInput1_change_1(event) {
   $w("#dataset1").onReady(() => {
 // get the current item from the dataset
 const currentItem = $w("#dataset1").getCurrentItem();
 // const product = $w('#productPage1').getProduct(); 

 // get the current average rating, number of ratings, and
 //total ratings for the current dataset item
 const average = currentItem.avg;
 const count = currentItem.numRatings;
 const total = currentItem.totalRatings1;

 // get the new rating from the ratings input
 const newRating = $w('#ratingsInput1').value;

 // calculate the new average rating based on the current
 //average and count
 const newAverageLong = (total + newRating) / (count +1);
 // Round the average rating to 1 decimal point
 const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
 
 // const productName = product.name;

 // set the dataset fields to the new average, total
 // ratings, and number of ratings
  $w('#dataset2').setFieldValues({
 'avg': newAverageShort,
 'totalRatings1': total + newRating,
 'numRatings': (count + 1)
  });
 
 // save the dataset fields to the collection
  $w('#dataset2').save()
   .catch((err) => {
    console.log('could not save new rating');
   });
 });
 }


RESULT -