Adding up values in the repeater

Hi,
I am trying to add the total amount of each nutrients that is in a repeater but can’t seem to get it right. Am starting with energy. I ended up getting this: NaN[object Object]undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined[object Object]undefinedundefinedundefinedundefinedundefinedundefinedundefined
Here is the code that I did:
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(“#dataset1”).onReady(() => {
let IeSum = 0;
let eSum = $w(“#repeater1”).data.reduce(
(accumulator, currentValue) => accumulator + currentValue.energyportion,
IeSum);
$w(“#ESum”).text = eSum.toString()
console.log(“addedEnergy”)
})
})
// and other filter codes below this…

Am I doing something wrong?

Thanks.

Maybe the energyportion field key is misspelled (it’s case sensitive). and/or maybe some items don’t have value in this field.

Yes, like J.D. said, be sure that " currentValue.energyportion " has really a value. Do some debuggin to be ensure.

Thank you J.D. and Brodwolfsky. I’ve checked and it wasn’t misspelled. I have also checked the fieldType and it is a number… I am guessing it is because I am filtering my repeater therefore I have that error? Could that be a possibility?

let eSum = $w(" #repeater1 ").data
console.log(eSum);

please post the log.

@benibrodi i get an error though…

@mydietarystrategies It’s the damn duplication bug. Use only one #repeater1 not #repeater1#repeater1

the log is:
Array(3)
0:
“{"_id":"item1"}”

1:
“{"_id":"item-j9sbjmlt"}”

2:
“{"_id":"item-j9sbjizh"}”

@mydietarystrategies so as you can see, you only have id there and no energyportion at all

You can also see that the repeater’s data is not taken from the dataset at all, because this is not the format of the IDs in Wix collection (unless you set these IDs using insert() )

@jonatandor35

@yisrael-wix told us that would be fixed this past Sunday… :roll_eyes:

@mydietarystrategies

NaN stands for not a number and like J. D. and Brod have pointed out, you are not returning a number value in your code, like J. D. says again too with you not having any value for energyportion.

Simply put as J. D. is trying to explain to you, is that you can’t add up numbers in code if you are not receiving any number values to begin with.

Also, as you are using a repeater, check out the API reference for it including the scope part of it too. https://www.wix.com/corvid/reference/$w.Repeater.html

Thanks @jonatandor35 @givemeawhisky @benibrodi . Based on what you all have said, cos I am filtering the repeater, I think I should be putting the code under the code where I am filtering the repeater instead of in the onReady? This way, there would be a value for the energy portion?

@givemeawhisky Yes. Apparently nowadays, no one gets too excited just because some tables are turned over.

@mydietarystrategies it looks like the repeater is not assigned to dataset1 at all.

1 Like

@jonatandor35 oh ok. When I previewed the page, the data from the dataset1 in the repeater is shown though.

@mydietarystrategies If you connected it via the editor it doesn’t make sense. According to the log you posted, the repeater contains items with ID only. So something is missing.

@jonatandor35 thanks for letting me know what the error means. :slight_smile: would need to figure out what is missing and what needs to be amended

Hi, I am getting the same thing, as seen in the photo below.

When I try to add the price from a repeater ( bbq and balcony, price 45 and 35), it gets many values. Were you able to fix the issue?

This is an old thread that is partially irrelevant to your question which makes it hard to follow. I’m closing it.
Please open a new post, describe the issue in details and add your code (if you have code)