Async update() vs getCurrentItem & setFieldValue

Trying to make a loop to automatically populate some database fields. The following async wixData.update() works:

wixData.query('database')
    .find()
    .then((results) => {
       let items = results.items;
       items.forEach((item) => {
          item.seoTitle = item.name + item.lengt + item.builder
          wixData.update('database', item)
      })
    })

However, sometimes some fields on some rows go missing (even though I am using the query THEN update). I’ve been trying to achieve the same thing using getCurrentItem & setFieldValue as follows:

let numOf = $w('#dataset').getTotalCount()

 $w('#dataset').getItems(0, numOf).then((results) => {
   let items = results.items;
      items.forEach((item) => {
         $w('#dataset').setFieldValues("seoTitle", item.name + item.length +             
                                        item.builder)
        $w('#dataset').save()
         })
     })

But I just can’t get it to work. It only writes in the first row only, and doesn’t move on to the next item. I’ve also tried using for loops too, same result.

I’d really appreciate someone pointing out what I’m doing wrong… Thanks.

@givemeawhisky can you help with this?

You should use .bulkUpdate() :
https://www.wix.com/corvid/reference/wix-data.html#bulkUpdate

Something like:

wixData.query('database')
    .find()
    .then((results) => {
       let items = results.items;
    items = items.map(e => {e.seoTitle = e.name + e.length + e.builder; return e;});
    return items;
    })
.then(items => {
    wixData.bulkUpdate('database', items);
})

(is lengt or length?)

[UPADETED]

@jonatandor35 Thanks for the reply. I’m not sure how bulkUpdate() is going to help me though. What I want to achieve is to use setFieldValues as @sailorcihan suggests in this post - https://www.wix.com/corvid/forum/community-discussion/wixdata-update-for-selected-fields . My code above seems to work, except it isn’t advancing the current item when setting the field, so it just overwrites the data in the first item. Any suggestions

@jonatandor35 Sorry, typo (bit like your retrun items ;-0 :-))!! Only kidding, sorry, I really appreciate the help as always J.D. Yes, I can see how this would work. But just for the exercise (and for others finding this post in the future I guess) I’d love to try and get it working with setFieldValues…I’ll be honest and say that update() / bulkUpdate scare me a little bit in overwriting everything, I have a big database with lots of items, and I’d really prefer to just set the fields that I need to…seems less risky somehow! Particularly if I make a coding error, since the only field I can overwrite is the one I call in setFieldValues(), that’s why I prefer that approach, if that makes sense?

@aquacruise yes I had a few typos :slight_smile: . Fixed now.
Re the setFieldValues, I never use it, so I can’t help (maybe someone else), but I don’t think the way you did it can work, because there’s no “currentItem” there.

@jonatandor35 Many thanks. I’ll prompt @givemeawhisky as they suggested this as a better way to update for selected fields. I did try using .next() to move to the next item but that didn’t work.

@aquacruise by the way, why don’t you use a beforeInsert() or beforeUpdate() hook for this task ?

@jonatandor35 YES YES YES! How do I like a thousand times!! I’m new to all this, so didn’t even know you could do that! Far simpler. For anybody else finding this post in the future, the solution is:

Have a quick look at https://support.wix.com/en/article/corvid-using-data-hooks . Go to your data manager, click hooks, and then add/edit code and choose before Insert. This will create a data.js file. Then I just added the following:

export function myDatabaseName_beforeInsert(item, context) {
    item.seoTitle = item.name + item.length + item.builder
    return item //Edit as per J.D. below
}

And that’s it! Every time I insert a new item in the database, it automatically does what I was trying to achieve. The only thing that is missing (for me) is that I will need to go and manually edit all the existing entries, since this only works on new items inserted into the database.

J.D. are you any good with routers, I’m having this small issue? https://www.wix.com/corvid/forum/community-discussion/using-router-for-seo-500-timeout-error (this is a different way for me or trying to solve the same problem, but can’t quite get it to work in all cases…)

Thanks again J.D.

@aquacruise you should add to the beforeInsert function: return item;
As for the existing items you can use 2 different shortcuts:

  1. export them to as a csv file, delete all records ( https://www.wix.com/corvid/reference/wix-data.html#truncate ), and import the csv.
  2. Write a beforeUpadte() hook with the same function, then run a 1-time query to retrieve all items and bulk update them (then you can remove the beforeUpdate() hook.
    (I have no experience with routers)