[SOLVED] view & vote code promblem

Hi there,
I found awesome video where Uval Blumenfeld (Product Manager Wix Code) show us how to add view and vote code to our site.

Wix Code | Creation Without Limits | Live Beta Launch - YouTube > 21:40 min.

I have all the code he showed us here:

import (ifNull) from 'public/utils.js';
var currentItem, ds;

export function dynamicDataset_onReady() {
	ds = $w ('dynamicDataset');
	currentItem = ds.getCurrentItem();
	
	let views = ifNull(currentItem.viewCount) + 1;
	let views = ifNull(currentItem.viewCount);
	
	ds.setFieldValue('viewCount', views);
	ds.setFieldValue('viewCount', votes);
	ds.save();
	
}

export function heart_onClick(event, $w) {
	$w('#heart').hide();
	let votes = ifNull(currentItem.voteCount) + 1;
	ds.setFieldValue('voteCount', votes);
	ds.save();
}

but problem is I don’t know the data which are import from public/utils.js
Or am I making mistake somewhere else?

1 Like

Hi Jakub!
That was just a little helper function I wrote myself so that the code would be cleaner while presenting. It’s a one-line function which returns the value of the parameter it’s passed, or 0 if it’s undefined.
There’s actually no need to put it in a separate file (unless you’re doing a live demo and need it to look pretty :slight_smile: ), simply paste this into your page code:

export function ifNull(a){
	return (a===undefined) ? 0 : a;
}

Hi Uval,
I’m glad you didn’t have a problem with your presentation as I have right now.
For some reason, it’s still not working for me. I probably miss a part of code somewhere.
If you still have your original code somewhere can you think you can share it with us please?
It would be awesome and very helpful.

Wish you nice day,
Jakub

I am too, having the same issue with this code working.

Following

Actually it looks like you have all the code right except for a small typo. When you’re setting the vote count you set it to the wrong field by mistake:

ds.setFieldValue('viewCount', views); 	
ds.setFieldValue('viewCount', votes);  
//                 ^^^^^^  this should set field voteCount and not viewCount!

Another thing that you need to think about is the component and field names. My code was written to support a specific page and collection. E.g. my dataset was called “dynamicDataset”, my fields in the collection were called ‘viewCount’ and ‘voteCount’, etc. If your names are different you’ll need to change the code.

Finally - What exactly isn’t working and what messages are you getting? That’ll help narrow it down. Are you getting any errors in the console? Paste them here and we can take a look.

For reference, here’s the exact code I used.
Good luck!
Uval

import {ifNull} from 'public/utils.js';
var currentItem, ds; 
 
export function dynamicDataset_onReady(event) {
	
	ds = $w('#dynamicDataset');
	currentItem = ds.getCurrentItem();

	//Increment the number of views on this item
	let views = ifNull(currentItem.viewCount) + 1;

	//In case the number of votes is null, set it to zero
	let votes = ifNull(currentItem.voteCount);

	ds.setFieldValue('viewCount', views);
	ds.setFieldValue('voteCount', votes);
	ds.save();
}

export function heart_onClick(event) {
	$w('#heart').hide();
	$w('#thanksBox').show('FoldIn');

	//increment the number of votes and save it
	let votes = ifNull(currentItem.voteCount) + 1;
	ds.setFieldValue('voteCount', votes);
	ds.save();
}

Hi Uval,

  1. I repaired my mistake in code but view and vote count still not working.
  2. I checked all the components a fields names and all is set well.
  3. I decided to copy your code and I checked once again components and fields names.

With all of that, I’m still making mistake somewhere.

On the picture below, you can see what problems are in preview mode.

When I saw public/utils.js module mistake I add this code to my current code:

 export function ifNull(a){ return (a===undefined) ? 0 : a; } 

But preview mode shows this:

Thanks,
Jakub

Hey Guys !
i figured out the problem !

you have to change the dataset on your page from read-only to Read & Write
and make sure that the database collection allows the users to write on it ( site memner or site member author)

i spent 2 hours to figure out what is the problem with it !

by the way, thnks Uval for the Video .

Hi Kenan,
I’m happy for you, but that’s not my case. I’ve done everything you wrote above but it’s not working.

Jakub

Jakub, looks like there’s an error with your import. I suggest completely removing the import line and just leaving the definition of ifNull within the page’s code itself.
Also, look at the error markings on the left side of the code editor. If there are any other errors showing up they might be affecting this issue.
If you’re still having trouble, paste the full page code here and I’ll take another look.

Well, it’s working finally! I made one big mistake …
I didn’t set up onReady function in Dataset properties. :frowning:
Anyway, thank you so much for all your support. You are amazing! Thank You!! =)

Jakub

Hey guys,
I am currently wanting to implement a “this item has been viewed more than 10 times in the last 24 hrs” feature to a dynamic page in my site, I have based my code on code from this thread and ‘If functions’ but have gotten nowhere so far, could someone please help me accomplish this feature?

More info on the feature:
A small box that shows up when view count (page loads) in a specific dynamic page item in the last 24 hrs is greater than 10.

Thanks in advance!

Hoping to revive this thread because I think it’s a really cool topic and task to accomplish on your own!

To add to the capabilities, I would like to be able to limit members to only one vote per dynamic page. As it stands, anyone can vote any number of times, therefore making the totals compromised.

My approach to this was to create a separate collection titled voteCheck, with two fields: userID and (in my case) classID. I have attached a data insert to the heart click function, with userID being found with current user, and classID being found with the ID field.

I am having issues making this whole process work together. onClick, I get an error that says 'Cannot read property ‘voteCount’ of undefined. And there is the issue of checking for duplicates.

If anyone has gotten anywhere further on this, or have any suggestions, I’d appreciate it! Hoping to work quick with this. All collections and datasets have correct permissions and read/write, here’s my code:

import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
var currentItem, ds;

$w.onReady( function () {

});

export function vectorImage16_click(event, $w) {
wixWindow.lightbox.close();
}

export function ifNull(a){ return (a===undefined) ? 0 : a; }

export function dynamicDataset_onReady(event) {

ds = $w('#dynamicDataset'); 
currentItem = ds.getCurrentItem(); 

//Increment the number of views on this item
let views = ifNull(currentItem.viewCount) + 1;

//In case the number of votes is null, set it to zero
let votes = ifNull(currentItem.voteCount);

ds.setFieldValue('viewCount', views); 
ds.setFieldValue('voteCount', votes); 
ds.save(); 

}

export function heartEmpty_click(event, $w) {
const classId = $w(‘#dynamicDataset’).getCurrentItem()._id
const userId = wixUsers.currentUser.id

wixData.insert('voteCheck', {'classId': classId, 'userId':userId}) 

. **catch** ((err) => { 
    console.log(err) 
}); 
$w('#heartEmpty').hide(); 
$w('#thanksBox').show('SlideIn'); 

//increment the number of votes and save it
let votes = ifNull(currentItem.voteCount) + 1;
ds.setFieldValue(‘voteCount’, votes);
ds.save();
}

Disregard, I figured it out!

For some reason my View Counts increment by 2 instead of 1. Any suggestions?

import wixData from ‘wix-data’;

//Voting and Viewing code

export function ifNull(a){
return (a===undefined) ? 0 : a;
}

var currentItem, ds;

export function ratingsDataset_ready() {
//Add your code for this event here:
ds = $w (‘#ratingsDataset’);
currentItem = ds.getCurrentItem();

//Increment the number of views on this item 
let views = ifNull(currentItem.viewCount) + 1; 

//In case the number of votes is null, set it to zero 
let votes = ifNull(currentItem.voteCount); 
ds.setFieldValue('viewCount', views); 
ds.setFieldValue('voteCount', votes); 
ds.save(); 

}

export function vectorHeart_click(event) {
//Add your code for this event here:
$w(‘#vectorHeart’).hide();
$w(‘#vectorHeartRed’).show();

 //increment the number of votes and save it 
let votes = ifNull(currentItem.voteCount) + 1; 
ds.setFieldValue('voteCount', votes); 
ds.save() 

}

All is working fine. But issue for me is it shows the like value incremented on first comment on the repeater when I click on any other comment. Please help me fix this. (I am using like feature on comments section)

My Page Code:

export function ifNull(a){ return (a===undefined) ? 0 : a; }

var currentItem, ds;

export function dataset2_ready() {

ds = $w('#dataset2'); 
currentItem = ds.getCurrentItem(); 

//In case the number of votes is null, set it to zero
let likecounter = ifNull(currentItem.likeCounter);

ds.setFieldValue('likeCounter', likecounter); 
ds.save(); 

}
export function heartwhite_click_1(event,$w) {

  $w('#heartwhite').hide(); 
   $w('#heartred').expand(); 

//increment the number of votes and save it
let likecounter = ifNull(currentItem.likeCounter) + 1;

ds.setFieldValue('likeCounter', likecounter); 
ds.save(); 

}

export function heartred_click(event,$w) {

 $w('#heartred').collapse(); 
 $w('#heartwhite').show(); 

//decrement the number of votes and save it
let likecounter = ifNull(currentItem.likeCounter);

ds.setFieldValue('likeCounter', likecounter); 
ds.save(); 

}