Update Collection issue with wixData.update

I’m trying to get (or query) data from my Collection and update an item on the same collection. I’ve used the API reference examples using both methods wixData.query and wixData.get followed by wixData.update to update the item.
I created a function in backend to call a third party service and need to run it 3 times on the client with 3 different inputs.
There is no problem with just running the function only one time. The issue is when I run it 3 times it only updates the third one. Appreciate for any help.

Here is my codes on client:


import wixUsers from 'wix-users';
import { getJSON } from 'wix-fetch';
import { createBuzz } from 'backend/confbuzz';
import wixLocation from 'wix-location';
import wixData from 'wix-data';

 let user = wixUsers.currentUser;
 let userId = user.id; 
 let isLoggedIn = user.loggedIn; 

$w.onReady(function () {

  $w("#text71").hide();

})

export function createBuzzer_click(event) {

 if  ($w("#buildingNumber").value === "") {
    $w("#text71").text = "Please enter the required field";
        $w("#text71").show();
 
  } 
 else if  ($w("#phone1").value === "") {
    $w("#text71").text = "Please enter the required field";
        $w("#text71").show();
 
  } 
 else {
    $w("#text71").hide();
 var phone1 = $w('#phone1').value;
 var phone2 = $w('#phone2').value;
 var phone3 = $w('#phone3').value;
 var desc1 = $w('#desc1').value;
 var desc2 = $w('#desc2').value;
 var desc3 = $w('#desc3').value;

    createBuzz(phone1,desc1)
     .then((forwarding) => {
        wixData.query("profile")
        .eq("_id", userId)
        .find()
          .then( (results) => {
 
    	let item = results.items[0];
        item.forwardingid1 = forwarding; 
        wixData.update("profile", item);
          console.log(forwarding);
          console.log(item);
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );

      })

    createBuzz(phone2,desc2)
      .then((forwarding) => {
        wixData.query("profile")
        .eq("_id", userId)
        .find()
          .then( (results) => {
 
 	let item = results.items[0];
        item.forwardingid2 = forwarding; 
        wixData.update("profile", item);
          console.log(forwarding);
          console.log(item);
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );

      })

    createBuzz(phone3,desc3)
      .then((forwarding) => {
        wixData.query("profile")
        .eq("_id", userId)
        .find()
          .then( (results) => {
 
 	let item = results.items[0];
        item.forwardingid3 = forwarding; 
        wixData.update("profile", item);
          console.log(forwarding);
          console.log(item);
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );

      })
 
  }
}

And here is my code on the backend:

import { ok, badRequest, notFound, serverError } from 'wix-http-functions';
import wixData from 'wix-data';
import { fetch } from 'wix-fetch';

export function createBuzz(phone, desc) {
 const url = 'https://...../api/v1/rest.php?api_username=';
 const username = '****';
 const password = '****'
 
 let forward = url + username + '&api_password=' + password + '&method=setForwarding&phone_number=' + phone + '&description=' + desc; 
 
console.log("Pre-fetch check.");
 
 return fetch(forward, {method: 'get'})
     .then(response => response.json())
    .then(json => json.forwarding);
 }

Did you tried with the for loop ?

Didn’t work. I had to redesign my code.

Hi,

Each item has the next two system fields:

  • _owner - holds wixUsers. currentUser. id and handles the access permissions.

  • _id - unique Id of an item.
    The code should look like:

let toUpdate = {
 	"_owner":wixUsers. currentUser. id,
        "_id":itemYouGotFromDB._id,
        ...rest of your fields
        }