I am new to coding with Corvid. Though I did a LOT of coding years ago in other technologies, I am brand new to this. I am working on a pet adoption web site and have many of the more complex things working nicely. It seems I am having trouble with the simplest things. Like, for example, displaying dynamic text on each pet page. I can easily title each pet dynamic page with the pet name. But, I am struggling to get a simple thing like a title that reads:
"Meet " [petName]!
I have the title in “text67”.
The field in the dynamicdataset I need it animalName.
let petName = $w(“#dynamicDataset”).getCurrentItem().animalName;//animalName is the field I want to get
$w(‘#text67’).text = "Meet " + petName + “!” ;
} );
} );
});
When I open the page with an animal record loaded, the title reads, “Meet undefined!”
I still have not gotten this working. One thing that puzzles me: when I look at other examples of similar code, I see different instances of “dynamicDataset” being used to reference the dataset. For example, I just looked at some code where they were using $w(" #clientDataset ").getCurrentItem().Name. It made me wonder if I am referencing the dataset incorrectly. (Note: I find the fact that there are multiple “names” for a dataset to be a bit confusing. Remember, I am a newbe.)
In my example: my dataset has been named “animalsDataset.” (see image)
The icon on the page shows 2 different “names” for the dataset. See image below:
The code seems to only allow me to reference #dynamicDataset and I do not see how others writing other code could end up with examples relating to dynamic sets with different names referenced in their getCurrentItem() code.
This seems like a very basic set of code and very basic functionality. I have to get it working.
The name that you use in the code is the name on the top of the dataset with the # in front of it, so your dataset in code should be ‘#dynamicdataset’.
if your dataset was setup on a normal page then it would just be called dataset, however as you have it on a dynamic page it is called dynamicdataset.
You can rename the elements id by going through the properties panel for the element, simply right click the element and choose properties. https://support.wix.com/en/article/corvid-working-with-the-properties-panel
If you closed the Properties panel, you can reopen it by selecting Tools in the Editor top bar, and selecting Properties Panel .
It is a whole lot easier if you give elements id names that match up with what you are using them for and why etc, especially when you come to use them in code.
$w.onReady(function () {
//add async before function so you can use await
$w("#dynamicDataset").onReady(async function () => {
//use await to make sure you get the item first
let item = await $w("#dynamicDataset").getCurrentItem();
//get the name of the animal
let petName = item.animalName
//set the text value to what you want
$w('#text67').text = "Meet " + petName + "!";
});
});
/* Or you could do this version */
$w.onReady(function () {
//add async before function so you can use await
$w("#dynamicDataset").onReady(async function () => {
//use await to make sure you get the item first
let item = await $w("#dynamicDataset").getCurrentItem();
//instead of making a petName variable, we will just use the item Data directly
$w('#text67').text = "Meet " + item.animalName + "!";
});
});
/*Even more condensed version
* Warning - I didn't try this so I am not 100% if it works lol.
*/
$w.onReady(function () {
//add async before function so you can use await
$w("#dynamicDataset").onReady(async function () => {
$w('#text67').text = "Meet " + await $w("#dynamicDataset").getCurrentItem().animalName + "!";
});
});
The first version is more similar to what you were doing. The second is a simplified version.
A couple of things to notice -
I got rid of the extra onReady() you had
I added async/await since most likely what was happening was the data wasn’t loaded until after you set the text. So it was technically undefined since it wasn’t loaded yet. Adding the async/await makes sure the data is loaded first. Learn more about it here
Edit - not sure why but it is displaying #dynamicDataset twice. just do it once.
Thank you. In my code the #dynamicDataset only appeared once. Not sure how it got duplicated. I copied and pasted your code and removed the duplicate #dynamicDataset (where it appeared) and am getting an error on $w(“#dynamicDataset”).onReady( async function () => { (see screen capture)
The error reads “Parsing Error: Unexpected token =>”
I am confused about why you think async is needed. Isn’t the whole point of onReady to make sure the dataset is, in fact, ready?
@benibrodi The dataset onReady() is there. That was in the original as well. The async/await is needed so that the value isn’t undefined. Await doesn’t wait for the dataset to be ready…
The duplication is a known ongoing forum bug which sometimes happens when you put any code into a code block.
Which as you can guess is very annoying for people trying to help with code examples and the users trying to copy and paste them with the double mistakes and not realising themselves if they are unaware themselves.
Great suggestions. Unfortunately, same result. Still not working. Again, this seems like a very basic kind of need. I can’t imagine there is not a simple, proven way to make this happen… as in constantly.
The issue turns out to not be with the code. My original code would work. The suggested code is better and I learned some things about Corvid along the way. At issue is the fact that the system seems to think there are 2 names for the petName field. Here is how it is in the actual database:
I decided to send the whole item to console.log and it came back with this:
The database shows the animal’s name as “animalName” and the log reports it as being “name.”
I know where this discrepancy came from: I have a standard coding rule that I do not use the fieldname “name” for anything other than a human name (particularly relevant in the line of work I am in). HOWEVER, I am porting this application over from another platform and used an intermediary transfer file to import the data. That data file had the field name “name” for the pets.
After the import process (during which the initial animal table was set up) I changed the pet name field to “animalName” so that the data complied with my normal rules. Wix/Corvid now seems to think I have 2 different field names for the same field. I’m guessing I just to dump the data and re-import, which is no big deal, it is just testing/preliminary data anyway. (In other words, Bella is not really available for adoption, in case anyone was interested).
I changed the code to reference item.name and it works perfectly.