import wixSecretsBackend from ‘wix-secrets-backend’ ;
import { getJSON } from ‘wix-fetch’ ;
export function getConditions ( city ) {
return wixSecretsBackend . getSecret ( “Clave_openWeather” )
. then (( secret ) => {
getJSON ( https://api.openweathermap.org/data/2.5/weather?q= ${ city } &appid= ${ secret } &lang=es&units=metric
)
. then (( json ) => { const temperatura = Math . floor ( json . main . temp ). toString ();
const clima = json . weather [ 0 ]. description ;
let result = [ temperatura ,
clima ];
**return** result })
. catch (( err ) => { console . log ( err + ' error en el json' )})
})
. catch ( err => console . error ( err + ' de Weather' ))
}
Hello. My first question is where is this code located?
My second is, have you tried debugging at different points in the function: for example checking the variables for the api call are correct
checking once the json is returned what it’s returning, etc etc
Let me know if you understand what I mean or if you need some resources for debugging your functions
The code is in the back-end and the function is called from the front-end; and The truth is that I don’t know exactly what it means to debug the function. I imagine that what you put is to try different variables given from the Json code and see if they all work or not… but I would need more data. Thank you.
Perfect. I didn’t want to make any assumptions about your knowledge.
Here is a very simple example of how to debug a backend function:
My backend code
import {getJSON} from 'wix-fetch';
export function getCatFacts1 () {
getJSON("https://cat-fact.herokuapp.com/facts")
.then(json => console.log(json))
.catch(err => console.log(err));
}
In the backend file you will see a little blue arrow to the left of your function, click this
Then in the tab that opens, click run
Then you will see the output in the right hand panel of your console.logs
I would start small if you aren’t sure where the problem is, meaning - just hit that weather API and console.log the response first. Once you are sure you are getting data back, you can move on
OK thank you. I’ll try it and let you know…
No problem and in case you need it…for the frontend you will need to modify your backend function once you are sure it is working. There are a few ways to do this, here is one:
Backend:
import {getJSON} from 'wix-fetch';
export async function getCatFacts1 () {
let facts = await getJSON("https://cat-fact.herokuapp.com/facts")
.then(json => {return json} )
.catch(err => console.log(err));
return facts;
}
Frontend:
import { getCatFacts1 } from 'backend/test';
$w.onReady(function () {
getCatFacts1().then(facts => {
console.log(facts);
})
.catch(error => {
console.log(error);
});
});
Thanks, I had to use the await async function because I couldn’t do it any other way. Also I had to remove the use of the secret because I couldn’t implement it with the async function. It seems to me that what caused problems was precisely the promise of secrecy. but I’m not sure, because the function didn’t read the parameter, and the data was arriving fine… Removing the secret and using the async function works perfectly for me. Thanks… but I would like to know if it is possible to use the secret in this way. Greetings