I can’t figure out why I’m getting this error!!
Here is my code:
export function saveForm_click(event) {
let toInsert = {
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
let toUpdate = {
"_id": $w("#clientID").value,
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
let ID = $w("#clientID").value;
wixData.query("onboardingForm")
.eq("_id", ID)
.find()
.then((results) => {
if (results.items.length > 0) {
wixData.update("onboardingForm", toUpdate)
.then((results) => {
let item = results;
})
.catch((err) => {
let errorMsg = err;
})
} else {
wixData.insert("onboardingForm", toInsert)
.then((results) => {
let item = results;
})
}
})
.catch((err) => {
let errorMsg = err;
})
}
Hello Dean,
what are you trying to do with your code?
Hi,
I am storing data from a form into the “Onboarding form” collection. However, additional data can be added to the form at any time. I am therefore querying the collection when a button is clicked that searches for the item _id. If the _id is found, then I want the collection to be updated and if no _id is found then I want the item to be inserted into the collection instead.
@dean41146
Try this one…
import wixData from 'wix-data';
$w.onReady(function () {});
export function saveForm_click(event) {get_data()}
function get_data() {
let ID = $w("#clientID").value;
wixData.query("onboardingForm")
.eq("_id", ID)
.find()
.then((results) => {
if (results.items.length > 0) {//here the code-part when an itemID was found! (existing already) ---> toUpdate-process
let firstItem = results.items[0];
start_UPDATE()
}
else { //here the code-part when an itemID was NOT found! (not existing yet) ---> toInsert-process
start_INSERTION()
}
})
.catch( (err) => {
let errorMsg = err;
});
}
function start_UPDATE() {
let toUpdate = {
"_id": $w("#clientID").value,
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
wixData.update("onboardingForm", toUpdate)
.then( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
}
function start_INSERTION() {
let toInsert = {
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
wixData.insert("onboardingForm", toInsert)
.then( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
}
I did not test it, ao you will perhaps to modify it a little bit to get it to work.
It is not he best solution, you can surely upgrade the code, but this should work.
@russian-dima Thank you for your help. The ‘collection does not exist’ error is resolved, but no update or insert takes place. Have no idea why! I have no messages in the console at all, not even the onboardingForm query results.
Hello again😁
If absolutely nothing happens, this could be the following reason.
Do some debugging…
Insert more console-logs! (on almost every-step of the code and look what goes wrong)
…like…
import wixData from 'wix-data';
$w.onReady(function () {});
export function saveForm_click(event) {get_data(), console.log("START")}
function get_data() { console.log("get-data started")
let ID = $w("#clientID").value;
console.log("ID")
wixData.query("onboardingForm") //<-----this is strange ??
.eq("_id", ID) // <-----this is strange ??(_id)??sure??
.find()
.then((results) => {
if (results.items.length > 0) {//here the code-part when an itemID was found! (existing already) ---> toUpdate-process
console.log(results)
let firstItem = results.items[0];
console.log(firstItem)
console.log(firstItem.title)
console.log(firstItem._id)
start_UPDATE()
}
else { //here the code-part when an itemID was NOT found! (not existing yet) ---> toInsert-process
start_INSERTION()
}
})
.catch( (err) => {
let errorMsg = err;
});
}
function start_UPDATE() {console.log("UPDATE-started")
let toUpdate = {
"_id": $w("#clientID").value,
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
wixData.update("onboardingForm", toUpdate)
.then( (results) => {console.log(results)
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
}
function start_INSERTION() {console.log("INSERTION-started")
let toInsert = {
"clientCoName": $w("#clientComp").value,
"firstName": $w("#clientFirstName").value,
"lastName": $w("#clientLastName").value,
"accountNumber": $w("#clientAccNum").value,
"registrationDate": $w("#registrationDate").value,
"step1": $w("#step1Complete").checked,
}
wixData.insert("onboardingForm", toInsert)
.then( (results) => {console.log(results)
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
}
Then look where your code stops.
Check also DATABASE-PERMISSIONS (set it for all user, to test it).
@russian-dima Seems like the problem was with my collection (not sure what exactly!). I removed the collection and created a new one and no more problem. Everything works great.
Thank you for your help - appreciated!
@dean41146
Do not forget, you can still improve this code.
It can be much better then this one!
But this will be your job ^^
Godd luck an happy coding.