Closed sheleoni closed 1 year ago
problem:
const session = await getServerSession();
returns null when called in api/user/userCollection
.
solved by:
adding headers: headers()
as a parameter when fetching api/user/userCollection.
const response = await fetch('http://localhost:3000/api/user/userCollection', {
method: 'GET',
headers: headers() // add this
});
reason for why this solves the issue: ref: https://github.com/nextauthjs/next-auth/issues/7423#issuecomment-1531064680 My understanding is:
Solved by 9fed7a5
Problem: fetch failed in production mode Try: https://github.com/vercel/next.js/issues/44062#issuecomment-1445183293
http://127.0.0.1/api/user/userCollection
NODE_TLS_REJECT_UNAUTHORIZED=0
to .env.local
ref: https://github.com/vercel/next.js/issues/44062#issuecomment-1482581149'use client'
None of the above has worked. Found this on stack overflow: https://stackoverflow.com/questions/76905386/typeerror-fetch-failed-during-next-js-project-build Option 1: fetch data directly from component without using /api route handler (?) Option 2: Ignore this fetch error on local build. It may work on deployment. Let's try.
↑ Didn't work.
Now reading: https://medium.com/@kaloyan_17221/fix-vercel-next-js-fetch-failed-from-undici-polyfill-8c66346c9c2f
search keyword:
OMG FINALLY SOLVED!!! PROBLEM WITH HEADER
const response = await fetch('http://localhost:3000/api/user/userCollection', {
method: 'GET',
// headers: headers() ◀️ delete this
});
Steps to debug:
1) log the headers()
2) read the error
OMG FINALLY SOLVED!!! PROBLEM WITH HEADER
But here comes a new issue occurring at http://localhost:3000/user/collection
After I delete headers()
, I won't be able to retrieve user ice-cream flavors anymore:
Only with headers()
am I able to retrieve use ice-cream flavour data. However, it breaks in production.
Breaking in production:
Update: Maybe api routes aren't available at build time!
Ref: https://stackoverflow.com/a/76569284 maybe fetching from own app's api endpoint won't work. maybe really try fetching the data within the component itself without using route handlers?
for now, give each user a unique ice-cream flavor (e.g. a string, like 'chocolate', 'strawberry', etc) users can view their own collection (not others') when they are signed in at http://localhost:3000/user/collection
/models
.save()
method to store these iceCream varieties in mongodblinking the collection of user to the collection ice-cream flavors
unlockedIceCream
. It should contain an empty array.ways to update existing schema
updateMany()
; orunlockedIceCream
field Conclusion: we will use updateMany for now.stuff to consider
viewing each users' owned ice-cream
in
user/collection
, allow users to see the ice-cream that they currently ownQA