#Cannot overwrite `user` model once compiled.

1 messages · Page 1 of 1 (latest)

shrewd lichen
#

Hi, i have an Remix app with Express, I have an api where it returns you if you have a session or not, the case is that always, always, from the loader function it returns false and not the session data (even if you are logged in)
Captures of the code:

#

Note:
Accessing the api route from the browser (whitout fetch or smth) works fine.

naive spire
#

If your remix app is hosted in the express app running at localhost:3000, you shouldn't be doing a fetch request to the same server. This is a weird / anti pattern. Since your remix app loader runs on the server, do that "me" logic in your "getLoadContext" and access it in your loader via loader({ context }).

#

If the express app is on a separate server, you'll need to forward on headers with something like:

function loader({ request }) {
  await fetch("http://localhost:3000/me", { 
    headers: request.headers
  });
shrewd lichen
#

Cannot overwrite user model once compiled.

#

Im going to rehuse this "forum" to my new error (wow!).

#

So, the error now is this:

#

This error occurs on this function:

#

Also, i attach my user model:

#

idk why occurs this error, so any help is appreciated.

naive spire
#

Ah, this is because in dev mode modules reload every request

#

You could do:

const toExport = global._userModel || (global._userModel = model("user", userModel))
export default toExport;

This will persist the model instance between

shrewd lichen
#

i will try

#

thx man,

#

this works

#

im fighting with the error 1 hour ago (i asked for help 10 minutes ago xD)

shrewd lichen
#

Im trying to handle errors with remix-auth, but, when i reload the page the alert error persists, how can i fix ?

#

this is my loader function

opaque oracle
#

If you don't remove the sessionErrorKey, it will persist. You probably want to use session.flash (set it when you get the error), then on the next request from your loader, session.get will return the error and remove it as well. If you're using cookie session storage, make sure you set the cookie header when returning from loader (to update your cookie)

action() {
  const session = await sessionStorage.getSession(...)
  // sets the value that will be removed on next request
  session.flash('sessionErrorKey', error)
  return json({ error }, {headers: {'set-cookie': await sessionStorage.commit(session)}})
}

loader() {
  const session = await sessionStorage.getSession(...)
  // gets the error and removes it
  const error = session.get('sessionErrorKey')
  // even though you only did get, you still need to 
  // commit and send updated cookie since the session 
  // was changed due to removed flash entry
  return json({ error }, {headers: {'set-cookie': await sessionStorage.commit(session)}})
}
shrewd lichen
#

Thanks