0

I'm making a web app using Wix and the API data won't display because of this error: Cannot read properties of undefined (reading 'orgao'). "Orgao" is one type of data the API provides. Here's the code:

FRONT-END

import {getCryptoCurrencyInfo} from 'backend/serviceModule';

$w.onReady(function () {
    // Write your JavaScript here

});

export function button1_click(event, $w) {
        getCryptoCurrencyInfo($w('#currencyInput').value)
        .then(currencyInfo => {
            $w('#result').text = "Orgao: " + currencyInfo[0].orgao + "\n" 
                                    + "Evento: " + currencyInfo[0].evento + "\n"
        });
}

BACK-END serviceModule.jsw

import {fetch} from 'wix-fetch';

export function getCryptoCurrencyInfo(month) {

    const url = 'https://transparencia.tce.sp.gov.br/api/json/despesas/2018/1';
    console.log("Url: " + url);

    return fetch(url, {method: 'get'})
        .then(response => response.json())

}

By the way, the names are a mix between a tutorial and my project, so don't get caught up on variables' names context. And the API doesn't require a token.

Help is much appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user92837
  • 29
  • 6
  • 1
    what do you get when you log `currencyInfo` to the console? can you provide a sample? – yochanan sheinberger Nov 25 '21 at 02:51
  • The URL you're using responds with a 404 status and body `{"message":""}` which is not an array. You should handle non-successful responses. See [Fetch: reject promise and catch the error if status is not OK?](https://stackoverflow.com/a/38236296/283366) – Phil Nov 25 '21 at 04:54
  • @yochanansheinberger Good one, it says: ```ReferenceError: currencyInfo is not defined```. Maybe ```currencyInfo``` it has something to do with the API of the tutorial I used, which is not available anymore. Any ideas on how to replace it? – user92837 Nov 25 '21 at 14:40
  • @Phil My comment earlier didn't go up. Thank you for your comment, I was actually playing with changing the url and left it incomplete! It works now, so it was a simple mistake. Thank you again. – user92837 Nov 25 '21 at 21:29

0 Answers0