adrianhajdin / healthcare

Build a healthcare platform that streamlines patient registration, appointment scheduling, and medical records, and learn to implement complex forms and SMS notifications.
https://jsmastery.pro
1.81k stars 432 forks source link

TypeError [ERR_INVALID_STATE]: Invalid state: chunk ArrayBuffer is zero-length or detached -- FIXED #32

Closed simseb closed 1 month ago

simseb commented 1 month ago

Node version: v18.19.1 "node-appwrite": "^13.0.0"

File upload fixed with these relevant lines:

import { InputFile } from 'node-appwrite/file'

const bufferFile = await identificationDocument?.get('blobFile').arrayBuffer()

if (identificationDocument) {
      const inputFile = InputFile.fromBuffer(
        bufferFile,
        identificationDocument?.get('fileName')
      )

      file = await storage.createFile(BUCKET_ID, ID.unique(), inputFile)
}
filhodoto commented 1 month ago

Cheers @simseb . I've been struggling with this error for a while. Typescript even tells us that InputFile.fromBuffer doesn't accept bufferFile type... but the fact is, this code works so thank you very much. I'll use this as a temporary solution.

Here's my code in case someone wants to add a little more type-check and silence their typescript.

node -> v18.19.1 node-appwrite -> ^13.0.0

// helper function in lib/utils.ts

export function isBlob(value: unknown): value is Blob {
  return value instanceof Blob;
}
if (identificationDocument) {
      // Get the Blob object from the identificationDocument
      const blob = identificationDocument?.get('blobFile');

      // Make sure we have a blob type of file in identificationDocument
      if (!blob || !isBlob(blob)) throw new Error('Blob file was not found');

      // !! Transform blob into buffer so we can use it and fix "TypeError [ERR_INVALID_STATE]" error
      // !! See solution here: https://github.com/adrianhajdin/healthcare/issues/32
      const bufferFile = await blob.arrayBuffer();

      const inputFile = InputFile.fromBuffer(
        // @ts-ignore
        bufferFile, // !! Typescript is correct in complaining. However at the moment Blob does not work so for now we keep it like this
        identificationDocument?.get('fileName') as string,
      );
}