Replacing items in a repeater

Hi there. I’m wanting to set up input fields to update a collection. However I want to overwrite the information in the collection if a field (in this case monthName) is the same as the input being submitted.

I have this code but keep getting an error and can’t work out what I need to do.

Be grateful for any help

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#submitButton").onClick(onFormSubmit);
});

function onFormSubmit() {
    const monthName = $w("#monthInput").value;
    const date = $w("#dateInput").value;
    const openTime = $w("#openTimeInput").value;
    const closeTime = $w("#closeTimeInput").value;

        wixData.query("openingTimes")
        .eq("month", monthName)
        .find()
        .then(results => {
            if (results.items.length > 0) {
    
                const existingRecord = results.items[0];
                const existingRecordId = existingRecord._id;

                wixData.update("openingTimes", existingRecordId, {
                    month: monthName,
                    date: date,
                    openTime: openTime,
                    closeTime: closeTime
                })
                .then(() => {
                    console.log("Record updated successfully");
                })
                .catch(error => {
                    console.log(error);
                });
            } else {
                wixData.insert("openingTimes", {
                    month: monthName,
                    date: date,
                    openTime: openTime,
                    closeTime: closeTime
                })
                .then(() => {
                    console.log("New record created successfully");
                })
                .catch(error => {
                    console.log(error);
                });
            }
        })
        .catch(error => {
            console.log(error);
        });
}

The error is here

wixData.update(“openingTimes”, existingRecordId, {
month: monthName,
date: date,
openTime: openTime,
closeTime: closeTime
})

and I get this error message

Argument of type ‘{ month: string; date: Date; openTime: string; closeTime: string; }’ is not assignable to parameter of type ‘WixDataOptions’. Object literal may only specify known properties, and ‘month’ does not exist in type ‘WixDataOptions’.

Many thanks for any help


Hi,

I believe there is a issue in the way the wixData.update() method was used.

This method accepts the _id as a key in the object representing the row you are updating (within the second parameter of the method). The third parameter is used for setting WixDataOptions such as suppressAuth and suppressHooks.

Provided Code

const existingRecord = results.items[0];
const existingRecordId = existingRecord._id;
wixData.update("openingTimes", existingRecordId, {
    month: monthName,
    date: date,
    openTime: openTime,
    closeTime: closeTime,
  })
  .then(() => {
    console.log("Record updated successfully");
  })
  .catch((error) => {
    console.log(error);
  });

Example Code Snippet

const existingRecord = results.items[0];
wixData.update("openingTimes", {
    _id: existingRecord._id,
    month: monthName,
    date: date,
    openTime: openTime,
    closeTime: closeTime,
  })
  .then(() => {
    console.log("Record updated successfully");
  })
  .catch((error) => {
    console.log(error);
  });

Try out this example code snippet and see if it helps solve your issue!