Open Vicidevs opened 2 months ago
use import { InputFile } from 'node-appwrite/file';
use import { InputFile } from 'node-appwrite/file';
and use InputFile.fromBuffer().
use import { InputFile } from 'node-appwrite/file';
this seems not to be working from my end
// patient.actions.ts
"use server";
import { ID, 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 { // Create new user -> https://appwrite.io/docs/references/1.5.x/server-nodejs/users#create 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) => { console.log("identificationDocument", identificationDocument); console.log("patient", patient);
try { // Upload file -> // https://appwrite.io/docs/references/cloud/client-web/storage#createFile let file; if (identificationDocument) { const inputFile = identificationDocument && InputFile.fromBuffer( identificationDocument?.get("blobFile") as Blob, identificationDocument?.get("fileName") as string );
file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}
// Create new patient document -> https://appwrite.io/docs/references/cloud/server-nodejs/databases#createDocument
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,
}
);
console.log("new Patient", newPatient);
return parseStringify(newPatient);
} catch (error) { console.error("An error occurred while creating a new patient:", error); } };
import { InputFile } from 'node-appwrite/file';
let file; if (identificationDocument) { const inputFile = identificationDocument && InputFile.fromBuffer( identificationDocument?.get("blobFile") as Blob, identificationDocument?.get("fileName") as string ); file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile); }
Hi bro can you share the full code, it seems Im not getting you
I encountered an import error for 'fs' module for the InputFile module. This is due to fs not being available in the browser as it only exists in nodejs environment. If you face the same issue, go to next.config.mjs file and update with the following:
use this const sdk = require('node-appwrite'); and befor any function of "node-appwrite" just right sdk => sdk.Query.equal("email", [user.email]) etc.
I encountered an import error for 'fs' module for the InputFile module. This is due to fs not being available in the browser as it only exists in nodejs environment. If you face the same issue, go to next.config.mjs file and update with the following:
Code source
This works!
use import { InputFile } from 'node-appwrite/file';
its worked for me, thanks
@biyiemmy @Vicidevs
node-appwrite13.0.0
in your utils file add
export const getBufferImage = async(fileInfo: File) => {
try {
const bytes = await fileInfo.arrayBuffer();
const bufferImage = Buffer.from(bytes);
return bufferImage;
} catch (error) {
throw error
}
}
inside patient.action.ts
export const registerPatient = async({ identificationDocument, ...patient}: RegisterUserParams) => {
try {
let file
if(identificationDocument) {
const docFile = identificationDocument?.get('blobFile') as File;
const fileName = identificationDocument?.get('fileName') as string
const bufferImg: Buffer = await getBufferImage(docFile);
const inputFile = InputFile.fromBuffer(bufferImg,fileName)
file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile)
}
const newPatient = await database.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)
// return
} catch (error: any) {
console.log("Erorr", error);
}
}
since the InputFile.fromBuffer()
and InputFile.fromBlob()
were having too many issues to handle on version compatibility. Converting the Blob to Buffer and passing it to storage does the job.
If anyone has a better solution let me know :+1:
Hey Guys! You just have to the file "use server" at the top of the file do get it work..
change your "node-appwrite" version and add this version
"node-appwrite": "^12.0.1",
Then add this InputFile.fromBlob()
instead of InputFile.fromBuffer()
instead of InputFile.fromBuffer() use File() constructor as follows it workes for me.
// REGISTER PATIENT export const registerPatient = async ({ identificationDocument, ...patient }: RegisterUserParams) => { try { // Upload file let file; if (identificationDocument) { const inputFile = new File( [identificationDocument?.get("blobFile") as Blob], identificationDocument?.get("fileName") as string );
file = await storage.createFile(BUCKET_ID!, ID.unique(), inputFile);
}
// Create new patient document
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); } };
change your "node-appwrite" version and add this version "node-appwrite": "^12.0.1", Then add this
InputFile.fromBlob()
instead ofInputFile.fromBuffer()
THIS WORKED!!!!!!!!!!!!!!! i have been banging my head since two days. re wrote the code fpr multiple times. this works T T. Thanks a ton!
Cannot find the InputFile, any alternative?