createresource why is this error even on a basic example

i dont know why event the basic example is error
No description
No description
No description
31 Replies
navi.ToTskie
navi.ToTskie8mo ago
No description
REEEEE
REEEEE8mo ago
use data()?.name, the resource is undefined initially
navi.ToTskie
navi.ToTskie8mo ago
But why on example on official website doesnt have '?'?
REEEEE
REEEEE8mo ago
🤷 typo maybe? What example were you looking at?
navi.ToTskie
navi.ToTskie8mo ago
still error no data displayed
Jasi
Jasi8mo ago
check what the fetch returns Suspense will only catch during load, not once the resource finished loading. So maybe your resonse.json() is undefined?
navi.ToTskie
navi.ToTskie8mo ago
yeah i forgot the await on that but still no data displayed
Jasi
Jasi8mo ago
hmm. does console.log(await response.json()) display data? inside the thisProject function
navi.ToTskie
navi.ToTskie8mo ago
i have solution on this before but for me i think this is not the right way. here the solution i made on other route
No description
navi.ToTskie
navi.ToTskie8mo ago
i bellieve this solution is so redundant
navi.ToTskie
navi.ToTskie8mo ago
this
No description
Jasi
Jasi8mo ago
then shouldn't it be data()[0].name? instead of data().name
navi.ToTskie
navi.ToTskie8mo ago
No description
navi.ToTskie
navi.ToTskie8mo ago
same error
navi.ToTskie
navi.ToTskie8mo ago
No description
navi.ToTskie
navi.ToTskie8mo ago
here is the whole code
Jasi
Jasi8mo ago
@navi.ToTskie you can put ``` around it to make it more readable. like: ```tsx your code ``` no with three ` yes, and now you can add tsx after the first three ` to add syntax highlighting :) on the same line like this: ```tsx your code ```
Jasi
Jasi8mo ago
No description
navi.ToTskie
navi.ToTskie8mo ago
how to new line here?
Jasi
Jasi8mo ago
shift + enter
navi.ToTskie
navi.ToTskie8mo ago
async function thisProject(id) {

const response = await fetch(
`${import.meta.env.VITE_API_ENDPOINT}/getIdProject`,{
method: 'POST',
body: JSON.stringify({
id:parseInt(id)
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})

return await response.json();
}
async function thisProject(id) {

const response = await fetch(
`${import.meta.env.VITE_API_ENDPOINT}/getIdProject`,{
method: 'POST',
body: JSON.stringify({
id:parseInt(id)
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})

return await response.json();
}
ahhh shttt hahaha thanks for i learn new on discord hahaha
export default function inProj(){
const params = useParams();
const [data] = createResource(params.id,thisProject)

return (

<CredentialUser>
<Body>
<main class="mt-5 pt-3">
<div class="container-fluid">

<Suspense fallback={<p>..loading</p>}>
<div>
<p>Name: {data()[0].name}</p>

</div>
</Suspense>


</div>
</main>

</Body>
</CredentialUser>
);
}
export default function inProj(){
const params = useParams();
const [data] = createResource(params.id,thisProject)

return (

<CredentialUser>
<Body>
<main class="mt-5 pt-3">
<div class="container-fluid">

<Suspense fallback={<p>..loading</p>}>
<div>
<p>Name: {data()[0].name}</p>

</div>
</Suspense>


</div>
</main>

</Body>
</CredentialUser>
);
}
Jasi
Jasi8mo ago
you're welcome :) This helps a lot when reading code. You're doing everyone a favor.
navi.ToTskie
navi.ToTskie8mo ago
yeah i always wanted to learn this before like coloring the code on discord
Jasi
Jasi8mo ago
But I have to disappoint you. I don't know whats wrong with your code thinkies
navi.ToTskie
navi.ToTskie8mo ago
anyways that is the code where it might configure it wrong? awp
Jasi
Jasi8mo ago
can you put data in a create effect and print its value?
createEffect(() => {
console.log(data())
}
createEffect(() => {
console.log(data())
}
navi.ToTskie
navi.ToTskie8mo ago
same error 😦 do you have this kind of error on createresource? i can solve it like this but it looks like redundant
Jasi
Jasi8mo ago
no remove the code in the template that produces this error. The console log in the effect should be working.
navi.ToTskie
navi.ToTskie8mo ago
createeffect does the same thing as
const [data,{refetch}] = createResource(params.id,thisProject)
refetch()
const [data,{refetch}] = createResource(params.id,thisProject)
refetch()
Jasi
Jasi8mo ago
I don't know what you mean by this .-.
navi.ToTskie
navi.ToTskie8mo ago
nvm thanks 😄
Want results from more Discord servers?
Add your server
More Posts