2021年3月28日星期日

Two errors for Nextjs: res.json is not a function and getserversideprops did not return an object

I'm trying to make an api request and I do get the data because I tried console logging it and it shows on the console but I still get errors. Here are the errors:

TypeError: res.json is not a function    Error: Your `getServerSideProps` function did not return an object. Did you forget to add a `return`?        

Here is my code:

export async function getServerSideProps(context) {    // API KEYS    const apiKeyLocationIQ = process.env.API_KEY_LOCATIONIQ;    const apiKeyWeather = process.env.API_KEY_WEATHER;    // API REQUEST OPTIONS    let cityLocationIQ = context.params.location;    let urlLocationIQ = "https://api.locationiq.com/v1/search.php?format=JSON&key="+apiKeyLocationIQ+"&q="+cityLocationIQ+"&limit=1";    // API REQUEST FOR LAT AND LON    try{      const res = await axios.get(urlLocationIQ).then((resLocation)=>{        return resLocation      })      const weather = await res.json();      return {        props: {          weather        }      }    }catch (error){      console.log( error);    }      }  
https://stackoverflow.com/questions/66848040/two-errors-for-nextjs-res-json-is-not-a-function-and-getserversideprops-did-not March 29, 2021 at 10:22AM

没有评论:

发表评论