Unsure what an error means

I’m working with placing objects inside an array that is then being placed in a database. There isn’t much documentation or help I can find on the internet for arrays used as field types, so if there’s anything anyone knows about please tell me! Whenever I use update or save to try to save the array to the item, it gives me this error and I’m not sure what it wants me to do or what’s wrong with it

Error: WDE0004: Failed to save [undefined] into [dbtsAddresses]. Items must be JavaScript objects.

This is my code:

export function btnSubmitNewService_click(event) {
 //Add your code for this event here: 

    let id = $w("#dynamicDataset").getCurrentItem()._id;

    let address = $w('#txtAddress').text;
    console.log(address);
    let date = $w('#dtDateOfService').value;
    console.log(date);
    let notes = $w('#inpNotesOnVisit').value;
    console.log(notes);

    let firstService = new Service();
    firstService.Address = address;
    firstService.Date = date;
    firstService.Notes = notes;

    let toUpdate;
    let allServices = [];

    wixData.query("dtbsAddresses").eq("_id", id).find()
        .then((results) => {
             if (results.length > 0) {
                allServices.concat(results.items[0].gdServices);
                allServices.push(firstService);
                console.log(allServices);

                toUpdate = {
                   "_id": id,
                   "gdServices": allServices
                };
                console.log(toUpdate);

                $w('#inpNotesOnVisit').value = "";
                $w('#dtDateOfService').value = new Date();
                console.log("done");
            }

        })
        .catch((err) => {
            console.log("query:  " + err);
        });

    wixData.update("dbtsAddresses", toUpdate).then((items) => {
        let item = items;
    }).catch((err) => {
        console.log("save:  " + err);
    });
}

Database:

I’ve been testing on the row with the square brackets in the services field.

Thanks for any help!

May be it is the timing issue with the promise, you need to wait for the first promise to fulfill before calling toUpdate, some thing like:

export async function btnSubmitNewService_click ( event ) {


await wixData . query ( “dtbsAddresses” ). eq ( “_id” , id ). find (

}

Hi! Sorry for the slow reply, I tried including the async, but it’s throwing a different error now. I also haven’t used async before, so not sure if I implemented it correctly.

new code:

export async function btnSubmitNewService_click(event) {
 //Add your code for this event here: 
     ...
    await wixData.query("dtbsAddresses").eq("_id", id).find()
        .then((results) => {
         ...
        .catch((err) => {
            console.log("query:  " + err);
        });

    wixData.save("dbtsAddresses", toUpdate).then((items) => {
            let item = items;
        }).catch((err) => {
            console.log("save:  " + err);
        });
}

new error:

Error: WDE0025: The dbtsAddresses collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor.

But dbtsAddresses did exist. You set it up, right? I don’t understand the complaint. You may have to do further investigation.
Another way to get by your problem is put the save code wixData . save ( …)
within . then (( results ) => { …} so you don’t need async and await any more.

The correct way is to use async and await.

I tried having it inside the original query, but it had the same error. Will do some more research!

make sure you put it after the line : console . log (done);