Silly Newbe Question

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.

My code in the onready looks like this:

$w.onReady( function () {

$w.onReady( () => {  
$w("#dynamicDataset").onReady( () => { 

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!”

What am I missing?

Hi Mike,

The code looks like it should work. One reason that it is undefined is that maybe animalName is not the actual field key. It is case sensitive.

Check if you have any value in your database or not (Live and Sandbox database don’t carry the same values unless synced)

Thanks Anthony. animalName is not the key field. It seems to me that I should be able to use ANY field in the current record. Right?

There is a current value. I added an input field to the page and linked it to animalName and I can see the animal name in that field.

Oh, and thanks for responding. I am finding this very odd.

@mike70099 Right.

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.

Have a quick look here at making a simple custom user input form etc.
https://www.wix.com/corvid/tutorial/how-to-create-a-custom-form-and-connect-it-to-a-database

Try one of these -

$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?

Thanks again. More confused now than ever.

@mike70099 Whoops, remove the => my bad.

JavaScript is just weird. The data might be ready however, the getCurrentItem() might not have returned yet and it kept going to the next line.

Hopefully this video is better at explaining it.

Here is another source of info - https://support.wix.com/en/article/corvid-working-with-promises

@benibrodi All of them use await…

@patrick ohh yes now I see!!!
But you can use the “dataset” better. There is an extra methode for that, so you don’t need the async/await.

Just use this:

$w("#myDataset").onReady( () => {
  console.log("The dataset is 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…

@patrick Ohhh… hoppla. I didnot ready the code very well. I haven’t seen you load a second Dataset.
Of course you are right!

Sorry for that. :slight_smile:

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.

It is something that is being looked at, honest!

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.

Problem solved! (Well, mostly)

Thank you @Patrick, @benibrodi and @givemeawhisky . I found the source of the problem. And, as you can see from this image, it is now working.


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.

Thanks again for your help.

What’s your website url so I can see all of the animals!