Bring to a page function the results of a Backend function

export function getPersonaSel(ID) //genera el json de la personas seleccionada existente en la tabla de la BD SQL
{
const url = ‘https://simplerestserver.azurewebsites.net/api/person/’ + ID + ‘/’;
console.log("Url: " + url);
return fetch (url, {method: ‘get’})
//.then(response => response.json()); //Los siguientes 3 renglones se podrían escribir en este unico
.then(httpResponse => {
//console.log(httpResponse)
if (httpResponse.status >= 200 && httpResponse.status < 300)
//if (httpResponse.ok)
{
return httpResponse.json();
}
else {
return Promise.reject(“No se encontró el registro”)
}
});
}
I need to get data from a Rest-API connected to a SQL Server DB.
To achieve this, I need a backend function that should give me one of these three possibilities:

  1. A json response with data I need.
  2. A Not Found alert
  3. A message that inform that it was an error in the process.
    In the page, I need a function that produce the following results, BASED ON THE RESULTS of the Backend results:
  4. Assign some of the json values to their input fields (FirstName, LastName)
  5. Assign to the Result field the Not Found Alert
  6. Assign to the Result field the Error occurred.
    How I get the Backend results in de Page Code?
    I have this Backend function (I suppose with many errors):
    export function getPersonaSel(ID)
    {
    const url = ‘https://simplerestserver.azurewebsites.net/api/person/’ + ID + ‘/’;
    console.log(“Url: " + url);
    return fetch (url, {method: ‘get’})
    .then(httpResponse => {
    if (httpResponse.status >= 200 && httpResponse.status < 300)
    {
    return httpResponse.json();
    }
    else {
    return Promise.reject(“Record not found”)
    } //Where I receive the information that it was a failure in the process??
    });
    }
    The page function (also with many errors):
    export function buttonVerSel_click(event)
    {
    let IdUsuario = $w(”#idInput").value;
    if (IdUsuario.length === 0)
    {
    $w(“#inputResultados”).value = “ID needed.”
    }
    else
    {
    getPersonaSel(IdUsuario)
    .then
    (selInfo =>
    {
    if (selInfo.ok)// I guess I have an error here
    {
    $w(“#inputIDSQL”).value = selInfo.ID
    $w(“#inputNombre”).value = selInfo.FirstName
    $w(“#inputApellidos”).value = selInfo.LastName
    $w(“#inputPayRate”).value = selInfo.PayRate
    }
    Else // How I difference between Not found an error?
    {
    $w(“#inputResultados”).value = “Algun error”
    }
    }
    );
    }
    }

Hey there
What do you get if you console.log the returned JSON you get from Azure API? Also what does selInfo show you when console.log that variable after the backend function is executed?

Andreas: When I console.log the returned JSON in the backend, I get the following:

Promise { _bitField: 0, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined, _progressHandler0: undefined, _promise0: undefined, _receiver0: undefined, _settledValue: undefined }

… and when I console.log the selInfo variable in the page function, I don’t get nothing.
Thank you,
Arturo

And you have tested the url with the value to ensure that it works to use it with GET as protocol?

Andreas: The URL works OK: When I give an existent ID I can get the data that I need without problem: I can see the FirstName and the LastName that corresponds with that ID. The main point than I need is that when I give an ID that doesn’t exist in the DB, I want that appears a message of NOT FOUND. I can’t achieve this. Or if there is a problem with the process, a message also inform about it.
I can retrieve this information in the backend function (json, status, or error) but how I get this data in the page function?

Try the way they return the json in this sample in the backend sample. The check json in backend and return the string you need Velo: Accessing 3rd-Party Services with the Fetch API | Help Center | Wix.com

I’m reading your link and now I have another question: In the first part of the link, the fetch is used in the page code. But when I read the information about fecht it said : “wix-fetch can be used in public and backend code for fetching resources from 3rd party services using HTTPS.”
( wix-fetch - Velo API Reference - Wix.com)
So I supposed that I can use fetch only in backend and not in client-side. Are not contradictory these two informations?

Please use the backend sample. Running fetch in page code is a bad idea

Andrea, in the sample you sent is not included the case that the register doesn’t exist in the API. In this example, if the City (that is the parameter) doesn’t exist, how I can show in the page a message that says “Not found”?

Avillarg,
Did you manage to return to the client side any message?
Roi.

Roi, the problem is that I don’t know how return this message. I can show the json data, bout I don’t know how show the “Not found” message. I mean I need to send two data form the backend: Rge json and the status. And I need to receive these two data in the client. Can you send me the code that I need for these two functions?

@Andreas Kviby: I need your help again! Could you please look my question in https://www.wix.com/corvid/forum/community-discussion/bulkremove
I’m very frustrated!