Back

#solved! Uncaught TypeError: Cannot read properties of undefined (reading 'replace')

  • 0
  • Web
Sayem Khan
14 Dec, 2023, 13:52

can someone help me to sort out the authentication problem in appwrite? when ever I try to call the account.createOAuth2Session/(or any other methods) an error that says Uncaught TypeError: Cannot read properties of undefined (reading 'replace') and the full error is

TypeScript
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at Client.setEndpoint (appwrite.js?v=026b3d50:707:89)
    at appwrite.js:5:6

here is my code appwrite.js

TypeScript
import { Account, Client } from "appwrite";

const client = new Client()
    .setEndpoint(import.meta.env.APPWRITE_ENDPOINT)
    .setProject(import.meta.env.APPWRITE_PROJECT_ID)

export const account = new Account(client);

export default client;
export { ID } from 'appwrite';

and the app.jsx is

TypeScript
import './App.css';
import { account } from './lib/appwrite';

function App() {
  const google = async () => {
    account.createOAuth2Session('google', 'http://localhost:5173/auth/google/success', 'http://localhost:5173/auth/google/failure');
  }

  return (
    <>
      <div>

      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={google}>Google</button>
      </div>
    </>
  )
}

export default App
TL;DR
The user had an error "Uncaught TypeError: Cannot read properties of undefined (reading 'replace')" in their code related to setting an endpoint in the Appwrite library. They were using Vite and had set the endpoint in their .env file. However, the error occurred when trying to access the endpoint using `import.meta.env.APPWRITE_ENDPOINT`. They were advised to prefix their environment variable with `VITE_` (e.g., `VITE_APPWRITE_ENDPOINT`) since that is how Vite makes environment variables available. The user made the change, replacing `import.meta.env` with a hardcoded value, and the error was resolved
ideclon
14 Dec, 2023, 14:40

What’s your code?

Drake
14 Dec, 2023, 15:48

Double check the endpoint you're passing is correct

Sayem Khan
14 Dec, 2023, 15:58

I've updated the post with code.

TypeScript
import { Account, Client } from "appwrite";

const client = new Client()
    .setEndpoint(import.meta.env.APPWRITE_ENDPOINT)
    .setProject(import.meta.env.APPWRITE_PROJECT_ID)

export const account = new Account(client);

export default client;
export { ID } from 'appwrite';
Sayem Khan
14 Dec, 2023, 16:02

Thanks, I replaced .setEndpoint(import.meta.env.APPWRITE_ENDPOINT) with .setEndpoint('endpoint_uri') and it works fine. but why the first item doesn't work? can you plz explain?

ideclon
14 Dec, 2023, 16:07

How were you setting APPWRITE_ENDPOINT?

Sayem Khan
14 Dec, 2023, 16:11

like this APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1 inside of .env in root directory

ideclon
14 Dec, 2023, 16:12

And how are you serving the site?

Sayem Khan
14 Dec, 2023, 16:13
ideclon
14 Dec, 2023, 16:14

That's where you're accessing it, but how are you serving that?

Sayem Khan
14 Dec, 2023, 16:15

are you talking about ``` "scripts": { "dev": "vite", "build": "vite build", "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" },

TypeScript
this?
I'm using vite to prepare the project and to run it locally I use `pnpm run dev`
ideclon
14 Dec, 2023, 16:17

So Vite would be the answer to the question I was asking

ideclon
14 Dec, 2023, 16:18

IIRC, Vite only makes available envvars which start with VITE_

ideclon
14 Dec, 2023, 16:19

So if you rename the var VITE_APPWRITE_ENDPOINT, it should work

Sayem Khan
14 Dec, 2023, 16:22

Thanks you so much for your explanation. I got it. And the problem is now fixed!

ideclon
14 Dec, 2023, 16:24

If your issue has been solved, you can mark this post as closed by adding “[SOLVED]” to the beginning of the title

Sayem Khan
14 Dec, 2023, 16:32

ok!

Sayem Khan
14 Dec, 2023, 16:33

#solved! Uncaught TypeError: Cannot read properties of undefined (reading 'replace')

ideclon
14 Dec, 2023, 16:33

@Sayem Khan Close - [SOLVED]

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more