craigtb
craigtb

Reputation: 667

Next.js getServerSideProps always undefined

I am new to Next.js and am trying to figure out how to use getServerSideProps. From my understanding I can essentially write server side code and it will execute when i load the page and the page will have the data passed in. What I am finding is that the data on the page is always undefined. I cant seem to figure out where I am going wrong. The code to get the records from my DB is executing and retrieving the records fine.

Can someone please give me some tips? Here is my code below...

export async function getServerSideProps() {
   
    const datastore = new Datastore();

    const query = datastore.createQuery('kind1')
        .limit(100);
    const [entities] = await datastore.runQuery(query);
    const e = entities.map(e => {
        console.log(e)
        return {
            "id": e[datastore.KEY].id,
            "name": e.name
        }
    });
    console.log(e)
    const obj = {
        props: {
            results:[1,2,3],
        },
    }
    console.log(obj.props.requests)
    return obj
}

export default function AdminDashboard( { data } ) {
    console.log(data)
return (
        <>
            
            .....
            
        </>
    )
}

Upvotes: 2

Views: 1774

Answers (1)

Saral Karki
Saral Karki

Reputation: 5412

In your getServerSideProps, you are setting results in props.

const obj = {
        props: {                      
            results:[1,2,3],    //Here
        },
    }

So, in your AdminDashBoard, your props will contain results instead of data which you are trying to destructure. So, destructure results instead

export default function AdminDashboard( { results } ) {
console.log(results)
return <></>
}

Here is official docs

Upvotes: 2

Related Questions