Hello! And apologies. This is a re-post. I think my first post fell thru the cracks. I am pretty sure the solution here is SUPER SIMPLE, but I cannot see it for the life of me, and cannot find another post here with the solution either - and I have scoured the forum. Though I am not new to coding, I am new to WIX coding.
I have a very simple update() call. The record to be updated is being identified by a Query() and the new data for the record is coming off a form on the page. I have checked my permissions and they look good: page permission : read/write and collection : anyone
Does anyone see a problem with this? - the update occurs on the onClick event for the Update Button on the page :
// UPDATE record with form contents
export function UpdateClick(event) {
const options = {
"suppressAuth": true, // just in case
"suppressHooks": true,
};
wixData.query("moment-practitioners")
.eq("naam", origSrchNaam) // find the record to update based on the ORIG search
.eq("postcode", origSrchPostcode) // criteria - user may have updated these fields
.find()
.then( (results) => {
if(results.items.length > 0) { // if found a match, then...
// assign form data to collection item
let item = results.items[0];
item.naam = $w("#iNaam").value;
item.voornaam = $w("#iVoornaam").value;
item.email = $w("#iWebsite").value;
item.website = $w("#iEmail").value;
item.gemeente = "ZippyTown"; // hardcoded for test purposes
item.postcode = "9933"; // hardcoded for test purposes
item.provincie = $w("#iProvincie").value;
item.tel1 = $w("#iTel1").value;
item.tel2 = $w("#iTel2").value;
item.beroepsactiviteit = $w("#iTxt1").value;
item.contexten = $w("#iTxt2").value;
item.specialisatie = $w("#iTxt3").value;
item.bijscholing = $w("#iTxt5").value;
item.mindfulnessBestaatUit = $w("#iTxt6").value;
item.trajectOmMindfulnessTrainer = $w("#iTxt7").value;
item.ervaring = $w("#iTxt8").value;
item.opleiding = $w("#iTxt4").value;
item.foto = $w("#iFoto").value;
item.Momentpractitioners = "/moment-practitioners/";
item._ID = hiddenID;
item.foto = hiddenFoto;
wixData.update("moment-practitioners", item);
} else {
// handle case where no matching items found
// DISPLAY no match text on screen
$w("#tNoMatchMsg").show();
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Though there could be other issues, the _ID should be in lower case. Since the update function depends on _id to find the right record, it’s as if you didn’t have an _id value at all in the item object when in it’s in upper case.
Thinking about this more, actually it still should write to the record. _id, the collection record’s true ID, is in the object still at the time you issue the update command. Both _id and _ID are in the object at that point.
It sounds like you have verified that the query is indeed returning a result, right?
@tony-brunsman Thank you kindly, Anthony, for the suggestion. I have change _ID to _id just to make it clean.
As for the query returning a result… 99% sure. For one: I am using the search criteria for the original search on the page (the search I used to retrieve the record data that I use to initially populate the update form on the page).
Also, I store the original search criteria values in global variables to use for the update query… also, in case the user edits the values of those fields in the update.
But, to be sure, I added debug code to the page to display (1) the search criteria associated with the update; (2) a message saying NO RECORD FOUND for the case where 0 records found; and (3) the error message in the case there is an execution error.
Here is the code as it stands now…below. I ran a test and NONE of the messages are being shown. I should have seen AT LEAST one of them.
Any thoughts?
note: if you want to test it, please search for: Naam: Andros Postcode: 9999
Below, look at the end for the comment: // query successful: Display search criteria
all my additions are there and below.
export function UpdateClick(event) {
const options = {
"suppressAuth": true,
"suppressHooks": true,
};
let errorMsg;
wixData.query("moment-practitioners")
.eq("naam", origSrchNaam) // find the record to update based on the ORIG search criteria - user may have updated these fields
.eq("postcode", origSrchPostcode)
.find()
.then( (results) => {
if(results.items.length > 0) {
let item = results.items[0];
item.naam = $w("#iNaam").value; // updated naam
item.voornaam = $w("#iVoornaam").value; // updated voornaam
item.email = $w("#iWebsite").value; // updated email
item.website = $w("#iEmail").value; // updated website
item.gemeente = "ZippyTown"; // updated gemeente
item.postcode = "9933"; // updated postcode
item.provincie = $w("#iProvincie").value; // updated provincie
item.tel1 = $w("#iTel1").value; // updated tel 1
item.tel2 = $w("#iTel2").value; // updated tel 2
item.beroepsactiviteit = $w("#iTxt1").value; // updated beroepsactiviteit
item.contexten = $w("#iTxt2").value; // updated contexten
item.specialisatie = $w("#iTxt3").value; // updated specialisatie
item.bijscholing = $w("#iTxt5").value; // updated bijscholing
item.mindfulnessBestaatUit = $w("#iTxt6").value; // updated bestaat
item.trajectOmMindfulnessTrainer = $w("#iTxt7").value; // updated traject
item.ervaring = $w("#iTxt8").value; // updated ervaring
item.opleiding = $w("#iTxt4").value; // updated opleiding
item.foto = $w("#iFoto").value; // updated foto
item.Momentpractitioners = "/moment-practitioners/"; // updated last name
item._id = hiddenID; // use previously-set hidden value
item.foto = hiddenFoto; // use previously-set foto value
wixData.update("moment-practitioners", item);
// query successful: display the search criteria
$w("#msgError").text = "srchNaam: " + origSrchNaam;
$w("#msgError2").text = "srchPC: " + origSrchPostcode;
$w("#msgError3").text = "hiddenID: " + hiddenID;
$w("#msgError").show();
$w("#msgError2").show();
$w("#msgError3").show();
} else { // no matching records found on query
// DISPLAY no match text on screen
$w("#msgNoRecord").show();
}
} )
.catch( (err) => { // there was an exe error - display the msg
$w("#msgError").text = err;
$w("#msgError").show();
} );
}
@web Strangely, only the first voorbeeld tekst: field updates fine. All the other values do not write.
Could you add the following line:
.then( (results) => {
console.log(results);
To be clear on the previous post, it would be better to remove the item._id = hiddenID line altogether. _id, the key to updating the correct record, is already in the item object.
@tony-brunsman OH! I was completely unaware that SOMETHING was updating – but I suspect it is not. When you reload the page, and BEFORE you search for anyone, you find that the placeholder text for that field (OPLEIDING) is not right - something wacky is going on. The placeholder text became the data that I used in my last update test. Does that make sense? Nevertheless, I don’t want to focus on that. Getting this thing to update is really my priority.
I have…
– removed the item._id line from the item object, and
– added the console log dump.
I tested it and the blasted thing will not display anything.
Here’s how it stands, now…
....
wixData.query("moment-practitioners")
.eq("naam", origSrchNaam) // find the record to update based on the ORIG search criteria - user may have updated these fields
.eq("postcode", origSrchPostcode)
.find()
.then( (results) => {
console.log(results);
if(results.items.length > 0) {
let item = results.items[0];
item.naam = $w("#iNaam").value; // updated naam
item.voornaam = $w("#iVoornaam").value; // updated voornaam
etc...
@web Nothing in the console. The attention should be on the query for the time being. So it can be ruled out as the problem. are the collection and field names in the right case? And also, are the two variables in the query letter for letter correct. I take it they are being assigned in the dropdown. Do the they have page scope? In other words, were they declared at the top of the page so they are available in the update button’s click event?
@tony-brunsman RESOLVED: I stumbled on the answer !!! I was putting debug code in there and at one point it occurred to me that the UPDATE function was simply NOT being called, so I turned my attention to the update BUTTON. And as it turned out, the function name in the code DID NOT MATCH the OnClick CALL associated with the button in the PROPERTIES BOX for the button.
I just tested it and it worked like a dream… and all the output started showing. WHEW!!!
I cannot thank you enough, Anthony, for your assistance here. It could have taken me days to find this error… but your question pushed us in the right direction.
Thank you, thank you, thank you… for your generosity. I am extremely grateful.