I recently started working on a NextJs project and encountered a challenge with Axios. In my VS code editor, I tried to access (response.data.sliders) but received an error stating "Property 'sliders' does not exist on type 'never'.ts(2339)". Despite seeing the 'sliders' in console.log(response.data), the error persisted. Feeling frustrated, I almost posted a question here seeking help when suddenly the solution dawned on me! It worked like magic, so I decided to share both the problem and the solution for others facing a similar issue. To provide clarity, I will include a screenshot and a snippet of my project code. This is what console.log(response.data) displayed
Here is a snippet of my code:
import axios from "axios";
const IndexPage = (props) => {
return (
<main>
<Section1 Section1Props={props.Section1Props} />
</main>
);
};
export async function getServerSideProps(context) {
try {
const response = await axios.get(urlGenerator("api/v1/frontend-configuration"));
const data = await response.data;
return {
props: {
Section1Props : data.sliders, // This is where the error occurred
},
}
} catch (error) {
console.log(error);
}
}
export default IndexPage;