Skip to content

FileReader error with Predictions on NextJS API routes. #8221

@ianwijma

Description

@ianwijma

Before opening, please confirm:

JavaScript Framework

React, Next.js

Amplify APIs

Predictions

Amplify Categories

predictions

Environment information


  System:
    OS: macOS 11.3.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 268.66 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.0/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Brave Browser: 89.1.22.71
    Chrome: 90.0.4430.93
    Edge: 90.0.818.51
    Firefox: 87.0
    Firefox Developer Edition: 88.0
    Safari: 14.1
  npmGlobalPackages:
    @aws-amplify/cli: 4.50.0
    localtunnel: 2.0.1
    npm: 6.14.11
    serverless: 2.35.0
    to: 0.2.9
    update: 0.7.4
    yarn: 1.22.10

Describe the bug

Using prediction within the NextJS API routes (which run on NodeJS ) throws the following error FileReader is not defined

Expected behavior

Although I'm fully aware NextJS API routes are not supported, I think it would be a great addition.

Reproduction steps

Setup a simple NextJS environment with Amplify:

  1. Install NextJS
  2. Initialize Amplify
  3. Initialize Predictions
  4. Create a NextJS API route that uses predictions
  5. See error

Code Snippet

const response = await fetch(url)
const file = await response.blob()

const { text } = await Amplify.Predictions.identify({
  text: {
    source: {
      file,
    },
    format: 'PLAIN',
  },
})

Log output

(node:45356) UnhandledPromiseRejectionWarning: ReferenceError: FileReader is not defined
    at /path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/Utils.js:37:22
    at new Promise (<anonymous>)
    at Object.blobToArrayBuffer (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/Utils.js:36:12)
    at /path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:112:25
    at new Promise (<anonymous>)
    at AmazonAIIdentifyPredictionsProvider.configureSource (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:90:16)
    at AmazonAIIdentifyPredictionsProvider.<anonymous> (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:168:51)
    at step (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:57:23)
    at Object.next (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:38:53)
    at fulfilled (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:29:58)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:45356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 9)
(node:45356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

I wasn't sure if this was a bug report of a feature request, so my bad it it needed to be a feature request!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Next.jsPredictionsRelated to Predictions categorySSRIssues related to Server Side Renderingfeature-requestRequest a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions