Update A Field In a Database Creates New Entry?

I have a very simple input form where I am trying to update a single field in a database. When I run in Preview mode, it Creates a New entry instead of updating the one in the Filter?

Here is the CODE:

import wixData from ‘wix-data’;
// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady( function () {
//Add your code for this event here:
});

export function submitButton_click(event, $w) {
$w(‘#dataset2’).setFilter(
wixData.filter()
.eq(‘dashno’, $w(‘#input4’).value)
);
console.log($w(‘#input4’).value);
console.log($w(‘#input6’).value);

$w(“#dataset2”).setFieldValue(“thumbnail”, $w(‘#input6’).value);
$w(“#dataset2”).save();
}

Here is the FORM:

I think you will have to wait for the setFilter promise to be done using a promise .then() and then you can set the field value and save it.

Could you please elaborate, better yet point to an example?

Hi CW:

What Andreas is saying is as follows:


Your code, outlined in red, is performing an asynchronous action. It is asking a data collection to go through its entries and find the matching records. It then sets up the collection to match the resulting items. This doesn’t happen immediately so setFilter returns something called a promise. The promise ‘promises’ to tell you what happened to your request at some point in the future but not now. When the filter completes it tells the promise you were given that it is ready and you can now trust the filtered result. When the promise completes (is resolved) .then() you can do something with the filtered data.

So I would advise you to read the setFilter() documentation and also get familiar with Promises so that the suggestion below makes sense.

Basically what you need to do is catch the promise result AND also any exceptions that might happen.

Your code should do what is expected if you add a .then() and a .catch()…

$w('#dataset2').setFilter(     
        wixData.filter()       
              .eq('dashno', $w('#input4').value)
)
// Process our promise result!
.then(() => {     
    console.log($w('#input4').value);
    console.log($w('#input6').value);
    // Check to make sure we have the item that we want to update ;-)
    let currentItem = $w("#dataset2").getCurrentItem();
    console.log(currentItem);
    // Defensive programming helps prevent unexpected side effects!
    if (!currentItem.hasOwnProperty('dashno') || currentItem.dashNo !==  $w('#input4').value) {
        // We have a problem with the filtered result this is a problem so lets throw and error 
        // for our catch below
        throw Error('filter failed');
    }
    // If we get this far we have the record we want to update
    $w("#dataset2").setFieldValue("thumbnail", $w('#input6').value);
    return $w("#dataset2").save(); // <====== NOTE this also returns a promise 
    // By returning it  we can add an additional .then() below if we want to or catch other exceptions
    // In the one catch() below.
})
// Because we returned the save() above we can now get hold of the
// Save result in a then call here
// wix-dataset.save() returns On fulfillment.  Object The saved item.
.then((savedItem) => {
    console.log(savedItem);
})
// Handle any exceptions raised by the process.
.catch((error) => {
        // Perform some sort of error handling
        // The setFilter docs states On rejection Error An error object is returned.
        console.log(error.message);
        console.log(error.stack); // Optional gives stack trace that lead to the error
});

Hope this is more clear :wink:

Thank you very much! I understand now. I took the code as you modified it and added to my page. While it APPEARED to work… (got the successful update message), fact is it added a new record to the wrong person. The registry entry is primary key “dash” and secondary “owner email”. So I deleted the duplicate and added another input field to the input form and added additional filter data. This time it DID work with the correct car, BUT it added a duplicate record instead of just updating the current record?

Here is the code as modified. I also used Properties Panel to name the fields for clarity…

import wixData from ‘wix-data’;
// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady( function () {
//Add your code for this event here:
});

export function submitButton_click(event, $w) {
$w(‘#dataset2’).setFilter(
wixData.filter()
.eq(‘dash’, $w(‘#Dash’).value)
.eq(‘email’, $w(‘#Email’).value)
)
// Process our promise result!
.then(() => {
console.log($w(‘#Dash’).value);
console.log($w(‘#Email’).value);
console.log($w(‘#URL’).value);
// Check to make sure we have the item that we want to update :wink:
let currentItem = $w(“#dataset2”).getCurrentItem();
console.log(currentItem);
// Defensive programming helps prevent unexpected side effects!
if (!currentItem.hasOwnProperty(‘dash’) || currentItem.dash !== $w(‘#Dash’).value ||
!currentItem.hasOwnProperty(‘email’) || currentItem.email !== $w(‘#Email’).value)

{ 

// We have a problem with the filtered result this is a problem so lets throw and error
// for our catch below
throw Error(‘filter failed’);
}
// If we get this far we have the record we want to update
$w(“#dataset2”).setFieldValue(“thumbnail”, $w(‘#URL’).value);
return $w(“#dataset2”).save(); // <====== NOTE this also returns a promise
// By returning it we can add an additional .then() below if we want to or catch other exceptions
// In the one catch() below.
})
// Because we returned the save() above we can now get hold of the
// Save result in a then call here
// wix-dataset.save() returns On fulfillment. Object The saved item.
.then((savedItem) => {
console.log(savedItem);
})
// Handle any exceptions raised by the process.
. catch ((error) => {
// Perform some sort of error handling
// The setFilter docs states On rejection Error An error object is returned.
console.log(error.message);
console.log(error.stack); // Optional gives stack trace that lead to the error
});
}

CW

I am not sure why you are getting a new record. The dataset docs say:

setFieldValues( )

Updates the values of a set of fields in the current item.
So this should only update the currentItem and should throw an exception if the current item isn’t the one you want. Do the console.log statements show that the currentItem is the one you should be updating? Did the console.log of the savedItem show the correct record? Remember if you have multiple records already you may be updating the wrong record.

Try checking the number of records that you filter returns and if there are more than one then the record might be being added somewhere else.

getTotalCount( )
Returns the number of items in the dataset that match its filter criteria.
getCurrentItemIndex( )
Returns the current item's index.
getItems( )
Returns the selected items.

Other than that I am not sure what can be wrong. Presumably you have other code creating the dataset items.

Ahhhh… I had the input fields of the form connected to the database. Then after reading thru the code again realized that the URL value gets saved. I guess since those fields were populated it created another record. Once I changed those input boxes to Not Connected, it is now working as expected.

Thanks So Much. Now I understand the “Promises, Promises”…

JD

I would like it if use can do a video of this, I have done what use have said but still not working as it should.
= updating but not replacing old data, making a new one insted.

I WOULD LOVE YOUR HELP <3

I have also same problem. if you are got the solution .please give me reply that solution

How to update a input filed the database and create a new entry with old records?

my code is updating the old value and does not create new entry.

export function input296_change(event) {
let toChange = event.target.value;
console.log(toChange);
$w(“#formDataset”).setFieldValue(“toAddress”, toChange);
$w(“#formDataset”).save()
}

This post is from 2018 and will be closed. Please start a new post with your question, code in a code block and anything you have tried.