I am currently following a tutorial on YouTube by JS Mastery about their HealthCare application using Next.js and Appwrite. I have obtained my API keys from the Appwrite cloud and added them to the .env.local file. However, upon running my project, I encounter the following error:
TypeError: Failed to construct 'URL': Invalid URL
at Users.create (users.mjs:77:17)
at createUser (patient.actions.ts:6:37)
at onSubmit (PatientForm.tsx:43:42)
at eval (index.esm.mjs:2256:23)
The code snippet for creating a user is as follows:
import { ID, Query } from "node-appwrite"
import { users } from "../appwrite.config"
export const createUser = async (user: CreateUserParams) => {
try {
const newUser = await users.create(
ID.unique(),
user.email,
user.phone,
undefined,
user.name
)
console.log(newUser)
} catch(error: any) {
if (error && error?.code === 409) {
const documents = await users.list([
Query.equal('email', [user.email])
])
return documents?.users[0]
} else {
console.log(error)
}
}
}
This segment of code exposes the user:
import * as sdk from "node-appwrite";
export const {
NEXT_PUBLIC_ENDPOINT: ENDPOINT,
PROJECT_ID,
API_KEY,
DATABASE_ID,
PATIENT_COLLECTION_ID,
DOCTOR_COLLECTION_ID,
APPOINTMENT_COLLECTION_ID,
NEXT_PUBLIC_BUCKET_ID: BUCKET_ID,
} = process.env;
const client = new sdk.Client();
client.setEndpoint(ENDPOINT!).setProject(PROJECT_ID!).setKey(API_KEY!);
export const databases = new sdk.Databases(client);
export const users = new sdk.Users(client);
export const messaging = new sdk.Messaging(client);
export const storage = new sdk.Storage(client);
Here are the contents of the .env.local file:
PROJECT_ID=************
API_KEY=************************************
DATABASE_ID=************
PATIENT_COLLECTION_ID=************
DOCTOR_COLLECTION_ID=************
APPOINTMENT_COLLECTION_ID=************
NEXT_PUBLIC_BUCKET_ID=************
NEXT_PUBLIC_ENDPOINT=https://cloud.appwrite.io/v1
I have verified that the API values are correct. To further confirm, I attempted substituting the variable names with the actual .env values and everything worked without issues.
client
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject("***************")
.setKey("*****************);
If you have any insights into what might be causing this issue or suggestions on how to resolve it, please do let me know. Any assistance would be greatly appreciated. Thank you.