[Solved] Dataset sync with repeater items

Hi, I need to compare the dataset2 items with the repeater items. Repeater is connected with the dataset1.

I’ve a field name “firm” in dataset2. Repeater is getting firm name from dataset1. I want to choose the interest rate according to firm stored in dataset 2. I don’t know how I should sync or compare this. Also, this can be possible with the reference field but I’m unable to get it even I read the wix document related to this.

wixData.query("transfirm").eq("_owner", wixUsers.currentUser.id).eq("name", $item('#text43').value)
  .find()
  .then( (results) => {
 let resultQuery = results.items.length;
    console.log(resultQuery);

Here I try to compare the field value with the current item. So, length should be one but it showing two. Means the second condition is not working. Help Me!

Maybe you have two records in the db collection that meet the criteria?

Where are you using this code? You will need to show context. What does the repeater look like?

Thanks for the reply. I’ve added the screenshots & code. Please check this & let me know the if any other information required.


import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
import wixSearch from ‘wix-search’;
import { local } from ‘wix-storage’;
import wixWindow from ‘wix-window’;

$w.onReady(() => {

$w("#totalsumdisplay").html = "<p style='font-size: 25px; font-style: avenir; text-align: right; color: black;'>Calculating...</p>";
$w('#dataset1').onReady(() => {

   $w("#repeater1").onItemReady( ($item, itemData, index) => { 

var a = new Date($item(‘#datePicker1’).value);
console.log(a);
var b = new Date($item(‘#datePicker2’).value);
console.log(b);

var y = a.getDate(); // starting date get & find days
var z = b.getDate(); // end
var m = a.getMonth();
var n = b.getMonth();
var r = a.getYear();
var s = b.getYear();
var monthdifference = n-m; //Month Difference
var yeardifference = s-r; //Year Difference

var principal = (itemData.amount);
let user = wixUsers.currentUser;
let userId = user.id;
//let filter1 =
if (wixUsers.currentUser.loggedIn){
//console.log($item(‘#text43’).value);
wixData.query(“transfirm”).eq(“_owner”, wixUsers.currentUser.id).eq(“name”, $item(‘#text43’).value)
.find()
.then( (results) => {
// let resultQuery = results.items;
results.items.forEach(() => {
if ($item(‘#text43’).text === “results.items[index].name”){
$item(‘#text43’).text = (results.items[index].rateofinterest).toString();
console.log(“Here is the rate”+results.items[index].rateofinterest);
}
else {
console.log(“J”);
}
})
} )
}

//$w(“#dataset2”).setFilter(wixData.filter().eq(“_id”, userId))

var rate = (itemData.rateofinterest);
console.log(“Your R te” +rate);
if (rate === null || rate === 0 || rate === undefined){
console.log(“Entered in If loop”);
var rate = 0;
$item(‘#text43’).html = “Please add your fixed rate of interest”;
var interest = 0;
$item(‘#text24’).html = “0.00”;
}
else {
var days = (parseFloat(($item(‘#text25’).text)));
var interest = (principalratedays)/36000;
$item(‘#text24’).text = interest.toString();
}
// $w(‘#input3’).value == days();
var getting = parseFloat($item(“#text19”).text);
if ($item(“#text20”).text === “Debit”) {
var value = (-1 * getting);
$item(“#text19”).html = “

” + value.toLocaleString(‘en-IN’, {
maximumFractionDigits: 2,
style: ‘currency’,
currency: ‘INR’
}); + “

”;
}
else {
var value = getting;
$item(“#text19”).html = “

” + value.toLocaleString(‘en-IN’, {
maximumFractionDigits: 2,
style: ‘currency’,
currency: ‘INR’
}); + “

”;

        }
   });

  $w('#dropdown1').onChange(() => { mysum2() });
  $w('#dropdown2').onChange(() => { mysum2() });

    $w('#resetfilter').onClick(() => {
        mysum2();
        $w("#dropdown1").value= "";
        $w("#dropdown2").value = "";
    })


})

});

Please realize that that is a lot of code to look through. I would suggest adding some console.log() statements to see what’s happening in the onItemReady() function.

One minor thing I noticed is that the onItemReady() function should not be inside the dataset onReady() function.

And please post your code, nicely formatted , in a code block. Do this:

I’ve used code snippet to post not sure why this didn’t work. Also, all the code is working fine if get rate of interest. I just need help with the comparing repeater item “Firm Name” with the dataset “name” field key to get the “rate of interest” for that firm. I’ve possible ideas for this: -

  • Comparing repeater item with dataset object (Don’t know how to compare object with string).
  • Using reference field. ( I don’t know how to use this.)

If you’ve any other method then I appreciate. Also, Thanks for looking out minor mistake & I will correct this. Hope this helps me with the missing dates fetching :slight_smile:

A few points and questions about this code:

results.items.forEach(() => {
   if ($item('#text43').text === "results.items[index].name") {
         $item('#text43').text = (results.items[index].rateofinterest).toString();
         console.log("Here is the rate"+results.items[index].rateofinterest);
   }
}

Are you trying to find the correct item in the results returned by the query? Well, the variable index is not defined for the forEach loop. It’s passed to the onItemReady() function. So, this forEach doesn’t loop through all of the query items.

It seems as you need something like this:

let queryItems = results.items;
queryItems.forEach((item, index) => {
   // do your check here
});

I may be wrong or confused, but just thought I’d point this out.

@yisrael-wix You got my point perfectly. Yes, I’m unable to use index that’s why tried second option. It showing “queryItems” is not a function. I hope you can make this working.


let queryItems = results.items;
queryItems((item, index) => {
if ($item( ‘#text43’ ).text === “queryItems[index].name” ) {
$item( ‘#text43’ ).text = (queryItems[index].rateofinterest).toString();
console.log( “Here is the rate” + queryItems[index].rateofinterest);
}
});


I’ve used divider as the code snippet doesn’t working.

@wix-expert-velo-cert To use code block, paste your code, highlight, and then. apply the code block as I showed.

Sorry, I had an error in my code, and I also need to correct your code:

let queryItems = results.items;
queryItems.forEach((item, idx) => {
   if ($item('#text43').text === item.name) {
      $item('#text43').text = item.rateofinterest;
      console.log("Here is the rate" + item.rateofinterest);
   }
});

I hope that should do it.

@yisrael-wix Hi, Thanks for the help. I’ve made a little change by adding for loop & worked perfectly.

let queryItems = results.items;
queryItems.forEach((item, idx) => {
 for (idx = 0; idx <= queryItems.length; idx++) {
 if ($item('#text18').text === item.name) {
                    $item('#text43').text = item.rateofinterest;
 //console.log("Here is the rate" + item.rateofinterest);
 var rate = parseFloat($item('#text43').text);
                    console.log(rate);
                }
            }

I just need help with the dates. As you can see screenshot in earlier reply it didn’t get loaded sometimes. Can you help me with that?

@wix-expert-velo-cert Your code does not set the DatePicker, it reads from it. You should be getting the date from the itemData variable in the onItemReady() function, and use that to set the DatePicker.

@yisrael-wix Hi, Thanks for the solution again. I was enjoying holidays after solution and now again a problem occurs. For the solution code, I’m getting these results.

let queryItems = results.items; 
 queryItems.forEach((item, idx)=>{
	for(idx =0; idx <= queryItems.length; idx++){
		if($item('#text18').text === item.name){ 
			$item('#text43').text = item.rateofinterest;
			//console.log("Here is the rate" + item.rateofinterest);
			var rate =parseFloat($item('#text43').text);                     
			console.log(rate);
		}
}