dataset's new() function not adding new blank in item

Hi, I have tried wix datasets and its great to use them.
I am trying to insert new blank record in my collection by the following code

$w("#dataset1").new()
.then(() => {

console.log("New item created");

$w("#dataset1").setFieldValues({
"title": email,
"memberid": userId,
"credit": 0
});

$w("#dataset1").save();
console.log("New item saved");
})
.catch((err) => {
let errMsg = err;
console.log("Error is : " + errMsg);
});

In the above code “#dataset1” is my dataset
my table has only 3 columns
title (text)
memberid (text)
credit (number)

When i run the code it doesn’t create new item and goes into error block
The console shows “Error is : DatasetError: Invalid index”
I don’t understand what is happening. Please help and Thanks in advance.

NOTE: My database collection has no records and I am trying to create a new record in onready function. dataset is connected to my collection with readwrite permission

I think your problem might be that the dataset isn’t ready. You mention that the code is in the onReady() function. I assume you mean the onReady() of the page. You’ll also need to put it in the onReady() of the dataset because the dataset loads after the page. Let me know if that works for you, or if you need some more guidance.

Also, I would suggest doing something like this using the wix-data API instead of wix-dataset. You could use the insert() function and not have to worry about a dataset at all.

Hi Sam,
Thanks for your reply. I will will definitely try wix-data API.
I tried to put the dataset related code in onReady() of dataset but still the error is same.

I am putting the full code of my page and a screenshot of database collection too.

//For full API documentation, including code examples visit http://wix.to/94BuAAs
import wixUsers from 'wix-users';
import {wixData}from 'wix-data';

$w.onReady(function () {	
        //get logged in user details
	let user = wixUsers.currentUser;
	let userId = user.id; // I need logged in userid to filter the dataset
	let email = user.getEmail();

       //filter dataset with "memberid" field in collection (match userId)
		$w("#dataset1").setFilter(wixData.filter()
		.startsWith("memberid", userId)
		)
		.then(() => {		
		// to check if there is already a record for logged in user
			let count = $w("#dataset1").getTotalCount(); 
			if (count <= 0) {				
				console.log("create new record for logged in user");
				$w("#dataset1").new()
					.then(() => {								
					console.log("New item created");
			
					//set new item default values
					$w("#dataset1").setFieldValues({
					"title": email,
					"memberid": userId,
					"credit": 0
					});
						
					$w("#dataset1").save();
					console.log("New item saved");
				})
				.catch((err) => {
					let errMsg = err;
					console.log("Error is : " + errMsg);
					});					
			} else {
				//Logic to update existing record of logged in user
			}			
		})
		.catch((err) => {
			console.log("Filter Error is : " +err);
		});
});

Hi Sam, Thanks for you suggestion of using wix-data api instead of wix-dataset api
I used wix-data API and got solution to what I was looking for.
I queried collection and checked if any record exists and then inserted new record.
Below is the code which is used.

//find if member credit item already exists by email id
	wixData.query("MemberCredits")
		.eq("title", userEmail)
		.find()
		.then((results) => {
			if (results.items.length <= 0) {
				//if record doesn't exist then enter new record with credit 0
				let toInsert = {
					"title": userEmail,
					"memberid": userId,
					"credit": 0
				};
				//insert in database
				wixData.insert("MemberCredits", toInsert)
					.then(() => {
						console.log("New credit inserted for " + userEmail);
					})
					.catch((err) => {
						console.log("Error while insertng member credit item : " + err);
					});
			}
		})
		.catch((err) => {
			console.log("Insert Query data err : " + err);
		});

Thanks Sam for your support. :slight_smile:

Good to see you got it worked out.

Just for the sake of completeness, the code you posted using the dataset still did not use the dataset’s onReady().

It should look something like this:

$w.onReady(function () {	
        //get logged in user details
	let user = wixUsers.currentUser;
	let userId = user.id; // I need logged in userid to filter the dataset
	let email = user.getEmail();
	
	$w("#dataset1").onReady( () => {
		//dataset stuff in here							
	} );
	
} );

Thanks Sam.
I will keep the above suggestion in mind for future work and implementation.