I’m using the below code and a repeater to make a comment section. At the top right corner of my page, I’m displaying the current user’s profile pic, I want that pic to appear on every comment made by the current user but it’s not working, it does it only for the first comment even if the comment doesn’t belong to the current user. For this scenario, ‘Afiliados’ is the name of the collection where the user’s profile info is stored and ‘#dataset1’ is the collection where the comments are being stored. Someone please help! Thanks.
import wixData from 'wix-data';
import wixWindow from 'wix-window';
import wixUsers from 'wix-users';
//Current user image
$w.onReady(function(){
if (wixWindow.rendering.env === "browser") {
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
currentUser.getEmail().then(email => {
wixData.query('Afiliados').eq('email', email).find()
.then(result => {
const item = result.items[0];
$w('#image31').src = item.image;
$w('#dataset1').setFieldValues( {
'ImageURL': $w('#image31').src
});
$w('#dataset1').save()
.then( () => {
$w('#dataset1').refresh();
});
});
});
}
}
});
Hi Manny,
It sets the picture only the first comment because you are not waiting for dataset to be ready as recommended in api reference .
I would suggest to use Reference field type for comment author. Then you wouldn’t need any code to display picture of comment author. Let me know if you need any help with that.
Thank you so much for your reply. I don’t wanna use reference fields because it’s not something that can be managed automatically. A bunch of pages will be created automatically when a new member sings up and I don’t wanna have to select the proper reference field every time.
Anyway, I added $w(“#dataset1”).onReady( () => { before $w(’ #dataset1 ').setFieldValues( { and it’s still not working, still doing the same thing. Why is it not working?
This is what my code looks like now:
import wixData from 'wix-data';
import wixWindow from 'wix-window';
import wixUsers from 'wix-users';
//Current user image
$w.onReady(function(){
if (wixWindow.rendering.env === "browser") {
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
currentUser.getEmail().then(email => {
wixData.query('Afiliados').eq('email', email).find()
.then(result => {
const item = result.items[0];
$w('#image31').src = item.image;
$w("#dataset1").onReady( () => {
$w('#dataset1').setFieldValues( {
'ImageURL': $w('#image31').src
});
$w('#dataset1').save()
.then( () => {
$w('#dataset1').refresh();
});
});
});
});
}
}
});
My solution to your problem would be to create Reference field in comments collection which would point to ‘Afiliados’ collection and would hold author of a comment. Then in a page where user can write comments, load required user from ‘Afiliados’ collection like you do in your code example and set it as the reference field in comments collection.
$w('#dataset1').setFieldValues( { 'author': item });
And in a page where you display comments just bind the picture to the picture of author field. Hope this will help.
Thank you so much, I figured it out. You’re the man!