Filling info from datasets

I am trying to auto-fill input boxes on a page using the getcurrent(),

although it seems to work briefly when i try the published site, it then dissapears.
And when i preview it, i get the following error code

’ An error occurred in one of datasetReady callbacks TypeError: Cannot read property ‘title’ of null ’

here is the code:

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

$w.onReady( function () {
//TODO: write your page related code here…

$w(“#dataset1”).onReady( () => {
$w(“#dataset2”).onReady( () => {

const currentItem = $w(“#dataset1”).getCurrentItem();
$w(“#input5”).value = currentItem.title;
$w(“#input4”).value = currentItem.forename;
$w(“#input7”).value = currentItem.detachment;
$w(“#input6”).value = currentItem.county;
$w(“#dropdown1”).value = currentItem.rank;

$w(“#dataset2”). new () //the second dataset is a read & write

const currentItem2 = $w(“#dataset2”).getCurrentItem();

$w(“#input8”).value = currentItem2.startDate;
$w(“#input9”).value = currentItem2.endDate;
$w(“#input10”).value = currentItem2.title;
$w(“#input12”).value = currentItem2.pocInformation;
$w(“#input11”).value = currentItem2.location;
$w(“#input13”).value = currentItem2.remarks;

});
});

});

For the null answer look at this recent previous post.
https://www.wix.com/corvid/forum/community-discussion/text-box-error

As for the get current item calls, please make sure that you are using the code functions correctly, see the api reference for more info.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem

Although, to be honest, you are much better using a combination of get current item and set field value or set field values for your code.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValues

THE ELEMENTS

The Page
User Inputs:
First Name Field: #input1
Last Name Field: #input2
Email Field: #input3
Phone Field: #input4
​Button Element: #Submit​​
The Database
Create a database Courses (dataset1).
​Recommended fields:
First Name ID: fname
Last Name ID: lname
Email ID: email
Phone ID: phone
Course Type ID: course
Course Date ID: date
Course Timing ID: time
Email ID: email

THE CODE

Page Code
// Auto-Fill Form //
$w.onReady(function () {
       $w("#input1").value = "Fred" // From database use: $w('#dynamicDataset').getCurrentItem().item //
       $w("#input2").value = "Flintstone"
       $w("#input3").value = "fredflintsone@wix.com"
       $w("#input4").value = "+1 123 123 1234"
});
​
// Submit Auto-Filled Items //
​
export function Submit_click(event, $w) {
        $w('#dataset1').setFieldValue('fname', $w('#input1').value);
        $w('#dataset1').setFieldValue('lname', $w('#input2').value);
        $w('#dataset1').setFieldValue('email', $w('#input3').value);
        $w('#dataset1').setFieldValue('phone', $w('#input4').value);
}

In this example, the first name, last name, email and phone number are hard coded.

You can either

  1. Change the auto-populated values (if your values are static) or
  2. Retrieve the values from a dataset. For example, $w(“#dataset1”).getCurrentItem().fieldName;

If you want to reset the form then just add a reset button and add this code to your page and change the element names to suit your own page.

THE CODE

Page Code
// Reset Button //
​
export function Reset_click(event, $w) {

    $w('#input1').value = "";
    $w('#input2').value = "";
    $w('#input3').value = "";
    $w('#input4').value = "";
    $w('#dropdown1').value = "Select Course";
    $w('#dropdown2').value = "Select Timing";

// Reset validation - Otherwise fields will show error //
    $w('#input1').resetValidityIndication();
    $w('#input2').resetValidityIndication();
    $w('#input3').resetValidityIndication();
    $w('#input4').resetValidityIndication();
    $w('#dropdown1').resetValidityIndication();
    $w('#dropdown2').resetValidityIndication();
}