Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error occurred while retrieving the patient details: SyntaxError: "undefined" is not valid JSON #49

Open
Ziexman opened this issue Aug 29, 2024 · 7 comments

Comments

@Ziexman
Copy link

Ziexman commented Aug 29, 2024

i get redirected to regestration page but nothing happens

@FeDev13
Copy link

FeDev13 commented Aug 29, 2024

Did you solve it? I have the same issue, some say it is related to the Appwrite version that is being used but I tried to upgrade to 13.0.0 and still get the same

@aln1234
Copy link

aln1234 commented Sep 11, 2024

I am also getting the same error.The query option is not working

@aln1234
Copy link

aln1234 commented Sep 11, 2024

hi, I was getting the same issue. The error is in the patient get server action. If you console log the patient data it is not fetching anything from the backend so instead of creating the appointment it is running the update appointment code for which it is asking for document Id. To resolve this you need to get the patient detail first. For this you need to update the node appwrite dependency to the latest version, it will solve the issue.

@Rafid13iit
Copy link

"use server";

import { ID, InputFile, Query } from "node-appwrite";
// import { InputFile } from 'node-appwrite/file';

import {
BUCKET_ID,
DATABASE_ID,
ENDPOINT,
PATIENT_COLLECTION_ID,
PROJECT_ID,
databases,
storage,
users,
} from "../appwrite.config";
import { parseStringify } from "../utils";

// CREATE APPWRITE USER
export const createUser = async (user: CreateUserParams) => {
try {
const newuser = await users.create(
ID.unique(),
user.email,
user.phone,
undefined,
user.name
);

return parseStringify(newuser);

} catch (error: any) {
// Check existing user
if (error && error?.code === 409) {
const existingUser = await users.list([
Query.equal("email", [user.email]),
]);

  return existingUser.users[0];
}
console.error("An error occurred while creating a new user:", error);

}
};

// GET USER
export const getUser = async (userId: string) => {
try {
const user = await users.get(userId);

return parseStringify(user);

} catch (error) {
console.error(
"An error occurred while retrieving the user details:",
error
);
}
};

// REGISTER PATIENT
export const registerPatient = async ({
identificationDocument,
...patient
}: RegisterUserParams) => {
try {
let file;
if (identificationDocument) {
const inputFile =
identificationDocument &&
// InputFile.fromBuffer(
InputFile.fromBlob(
identificationDocument?.get("blobFile") as Blob,
identificationDocument?.get("fileName") as string
);

  file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}

const newPatient = await databases.createDocument(
  DATABASE_ID!,
  PATIENT_COLLECTION_ID!,
  ID.unique(),
  {
    identificationDocumentId: file?.$id ? file.$id : null,
    identificationDocumentUrl: file?.$id
      ? `${ENDPOINT}/storage/buckets/${BUCKET_ID}/files/${file.$id}/view??project=${PROJECT_ID}`
      : null,
    ...patient,
  }
);

return parseStringify(newPatient);

} catch (error) {
console.error("An error occurred while creating a new patient:", error);
}
};

// GET PATIENT
export const getPatient = async (userId: string) => {

// Log for debugging
// console.log("USER_ID:",userId);
// console.log("PATIENT_COLLECTION_ID:", PATIENT_COLLECTION_ID);

try {
const patients = await databases.listDocuments(
DATABASE_ID!,
PATIENT_COLLECTION_ID!,
[Query.equal("userId", [userId])],
);

// console.log("Retrieved patients: ", patients);

return parseStringify(patients.documents[0]);

} catch (error) {
console.error(
"An error occurred while retrieving the patient details:",
error
);
}
};

@Rafid13iit
Copy link

"use server";

import { ID, InputFile, Query } from "node-appwrite"; // import { InputFile } from 'node-appwrite/file';

import { BUCKET_ID, DATABASE_ID, ENDPOINT, PATIENT_COLLECTION_ID, PROJECT_ID, databases, storage, users, } from "../appwrite.config"; import { parseStringify } from "../utils";

// CREATE APPWRITE USER export const createUser = async (user: CreateUserParams) => { try { const newuser = await users.create( ID.unique(), user.email, user.phone, undefined, user.name );

return parseStringify(newuser);

} catch (error: any) { // Check existing user if (error && error?.code === 409) { const existingUser = await users.list([ Query.equal("email", [user.email]), ]);

  return existingUser.users[0];
}
console.error("An error occurred while creating a new user:", error);

} };

// GET USER export const getUser = async (userId: string) => { try { const user = await users.get(userId);

return parseStringify(user);

} catch (error) { console.error( "An error occurred while retrieving the user details:", error ); } };

// REGISTER PATIENT export const registerPatient = async ({ identificationDocument, ...patient }: RegisterUserParams) => { try { let file; if (identificationDocument) { const inputFile = identificationDocument && // InputFile.fromBuffer( InputFile.fromBlob( identificationDocument?.get("blobFile") as Blob, identificationDocument?.get("fileName") as string );

  file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}

const newPatient = await databases.createDocument(
  DATABASE_ID!,
  PATIENT_COLLECTION_ID!,
  ID.unique(),
  {
    identificationDocumentId: file?.$id ? file.$id : null,
    identificationDocumentUrl: file?.$id
      ? `${ENDPOINT}/storage/buckets/${BUCKET_ID}/files/${file.$id}/view??project=${PROJECT_ID}`
      : null,
    ...patient,
  }
);

return parseStringify(newPatient);

} catch (error) { console.error("An error occurred while creating a new patient:", error); } };

// GET PATIENT export const getPatient = async (userId: string) => {

// Log for debugging // console.log("USER_ID:",userId); // console.log("PATIENT_COLLECTION_ID:", PATIENT_COLLECTION_ID);

try { const patients = await databases.listDocuments( DATABASE_ID!, PATIENT_COLLECTION_ID!, [Query.equal("userId", [userId])], );

// console.log("Retrieved patients: ", patients);

return parseStringify(patients.documents[0]);

} catch (error) { console.error( "An error occurred while retrieving the patient details:", error ); } };

Downgrade the node-appwrite version to 12.0.1 and edit this part of the code that I have given

@AbhinavSharma486
Copy link

Downgrade the node-appwrite version to 12.0.1 and edit this part of the code that I have given

'use server';

import { ID, Query, InputFile } from "node-appwrite";
import { BUCKET_ID, DATABASE_ID, databases, ENDPOINT, PATIENT_COLLECTION_ID, PROJECT_ID, storage, users } from "../appwrite.config";
import { parseStringify } from "../utils";
export const createUser = async (user: CreateUserParams) => {
try {
const newUser = await users.create(
ID.unique(),
user.email,
user.phone,
undefined,
user.name
);

return parseStringify(newUser);

} catch (error: any) {
if (error && error?.code === 409) {
const existingUser = await users.list([
Query.equal("email", [user.email])
]);

  return existingUser.users[0];
}
console.error("An error occurred while creating a new user:", error);

}
};

export const getUser = async (userId: string) => {
try {
const user = await users.get(userId);

return parseStringify(user);

} catch (error) {
console.error(
"An error occurred while retrieving the user details:",
error
);
}
};

export const registerPatient = async ({ identificationDocument, ...patient }: RegisterUserParams) => {
try {
let file;

if (identificationDocument) {
  const blob = identificationDocument?.get('blobFile') as Blob;
  const fileName = identificationDocument?.get('fileName') as string;

  // Convert Blob to ArrayBuffer and then to Buffer
  const arrayBuffer = await blob.arrayBuffer();
  const buffer = Buffer.from(arrayBuffer);

  const inputFile = InputFile.fromBuffer(
    buffer,
    fileName
  );
  file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}
const newPatient = await databases.createDocument(
  DATABASE_ID!,
  PATIENT_COLLECTION_ID!,
  ID.unique(),
  {
    identificationDocumentId: file?.$id || null,
    identificationDocumentUrl: `${ENDPOINT}/storage/buckets/${BUCKET_ID}/files/${file?.$id}/view?project=${PROJECT_ID}`,
    ...patient,
  }
);
return parseStringify(newPatient);

} catch (error) {
console.log("Error in registerPatient", error);
}
};

@mayurbagade456
Copy link

The error is sometimes happen due to when first time registering the user:
Register form =>
where we check is patient is already exist getPatient(userId) : redirect to new appointment
but we're trying to newly register on that time and hence our userId is used in that function without any cause and hence that error comes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants