Hi,
I have an issue where I was unable to update the correct record in the collection if I am looping through multiple items and calling external webservice API to get some results to update to the respective records.
The update will update certain records with the correct data but some records are not updated with the correct data and some records are not updated at all even though it should be updated.
Below code in written in the main page to call my backend code to run the data query and update:
import { QueryUpdateStatusFunction } from ‘backend/collection’ ;
$w . onReady ( function () {
// Write your JavaScript here
const myCollection = “recycleOrder” ;
QueryUpdateStatusFunction ( myCollection )
. then (( results ) => {
console . log ( "status: " + results );
})
. **catch** (( error ) => console . log ( error ));
});
Below code is written in the backend to query data to get all items from collection and loop through the items and call the web service to get status and other fields updates from external website and update the data retrieved from the webservice call into the collection:
//backend/collection.jsw
import wixData from ‘wix-data’ ;
import { GetApiData } from ‘backend/fetch’ ;
export async function QueryUpdateStatusFunction ( myCollection ){
let results = await wixData . query ( myCollection )
.eq(“vinrequestid”, “109”)
.or(
wixData.query(myCollection)
.eq(“vinrequestid”, “039”)
)
.or(wixData.query(myCollection)
.eq(“vinrequestid”, “038”)
)
. find ();
**let** allItems = results . items ;
**while** ( results . hasNext ()){
results = **await** results . next ();
allItems = allItems . concat ( results . items );
}
**const** url = "url" ;
**let** item ;
**let** payloadData ;
**let** payloadDataJson ;
**const** options = {
"suppressAuth" : **true** ,
"suppressHooks" : **true**
};
**let** returnResults ;
**for** ( **let** i = 0 ; i < allItems . length ; i ++){
item = allItems [ i ];
payloadData = {
"RequestId" : item . vinrequestid
};
payloadDataJson = JSON . stringify ( payloadData );
GetApiData ( url , payloadDataJson )
. then (( results ) => {
**switch** ( results . StatusId ){
**case** 0 :
item . status = "A" ;
**break** ;
**case** 1 :
item . status = "B" ;
**break** ;
**case** 2 :
item . status = "C" ;
**break** ;
**case** 7 :
item . status = "D" ;
**break** ;
**case** 3 :
item . status = "E" ;
**break** ;
**case** 5 :
item . status = "F" ;
**break** ;
**case** 6 :
item . status = "G" ;
}
**if** ( results . Pin ){
item . status = "H" ;
item . pincode = results . Pin ;
}
**if** ( results . PinExpiryDate ){
item . pinexpiry = **new** Date ( results . PinExpiryDate );
}
wixData . update ( myCollection , item , options )
. then (( updateResults ) => updateResults );
});
}
returnResults = allItems . length ;
**return** returnResults ;
}
Below code in written in the backend to call external webservice and get the response:
//backend/fetch.jsw
import { fetch } from ‘wix-fetch’ ;
export function GetApiData ( url , payload ){
return fetch ( url , {
method : “post” ,
headers : {
“Content-Type” : “application/json”
},
body : payload
})
. then (( httpResponse ) => {
if ( httpResponse . ok ){
return httpResponse . json ();
} else {
return Promise . reject ( “Error” );
}
});
}