parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Error: upload.arrayBuffer is not a function

Open VKlapan opened this issue 3 years ago • 27 comments

New Issue Checklist

Issue Description

When I try to upload a file (via Parse Dashboard GraphQL API Console) I receive an error: "upload.arrayBuffer is not a function". Do not understand - what is wrong?

Steps to reproduce

Try to check file uploading via Parse Dashboard GraphQL API Console. Query: mutation createFile($file: Upload!) { createFile(input: {upload:$file}) { fileInfo { name url } } } Variables - base64 string: { "file":"V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=" }

Actual Outcome

Response: { "errors": [ { "message": "upload.arrayBuffer is not a function", "locations": [ { "line": 2, "column": 3 } ], "path": [ "createFile" ] } ], "data": { "createFile": null } }

Expected Outcome

Environment

Server

  • Parse Server version: 6.0.0
  • Operating system: Linux Mint 20.2 Cinnamon and docker
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: mongo:4
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): local

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): JavaScript
  • SDK version: "@types/parse": "^2.18.13"

Logs

server config:

` Successfully compiled 1 file with Babel (297ms). SERVER CONFIG Configuration loaded from /parse-server/config/server.js warn: DeprecationWarning: The Parse Server option 'allowClientClassCreation' default will change to 'false' in a future version. warn: DeprecationWarning: The Parse Server option 'allowExpiredAuthDataToken' default will change to 'false' in a future version. RUN Parse Server Cloud allowClientClassCreation: true allowCustomObjectId: false allowExpiredAuthDataToken: true allowHeaders: ["X-Apollo-Tracing"] appId: parse-template appName: Parse Template cacheMaxSize: 10000 cacheTTL: 5000 cloud: /parse-server/cloud-dist collectionPrefix: customPages: {"invalidLink":"http://localhost/invalid-link","invalidVerificationLink":"http://localhost/invalid-verification-link","passwordResetSuccess":"http://localhost/password-reset-success","choosePassword":"http://localhost/choose-password"} databaseURI: mongodb://db/parse defaultLimit: 100 directAccess: true emailAdapter: {"options":{"service":"SMTP","fromAddress":"dev@.com","user":"dev@","password":"","host":"mail..com","port":587,"isSSL":false,"templates":{"resetPassword":{"path":"/parse-server/templates/reset-password","subject":"Password reset"},"verifyEmail":{"path":"/parse-server/templates/verify-email","subject":"Email verification"}}}} emailVerifyTokenReuseIfValid: false emailVerifyTokenValidityDuration: 7200 enableAnonymousUsers: true enableExpressErrorHandler: false enforcePrivateUsers: true expireInactiveSessions: true filesAdapter: {"_encryptionKey":null,"_filesDir":"/parse-server/files"} fileUpload: {"enableForPublic":true,"enableForAnonymousUser":true,"enableForAuthenticatedUser":true} graphQLPath: /graphql graphQLSchema: /parse-server/schema.graphql host: 0.0.0.0 idempotencyOptions: {"ttl":300,"paths":[]} liveQuery: {"classNames":[]} logLevel: warn logLevels: {"triggerAfter":"info","triggerBeforeError":"error","triggerBeforeSuccess":"info"} logsFolder: ./logs maintenanceKeyIps: ["127.0.0.1","::1"] masterKey: REDACTED masterKeyIps: ["172.30.0.0/16"] maxUploadSize: 4000mb mountGraphQL: true mountPath: / mountPlayground: false objectIdSize: 10 pages: {"enableRouter":false,"enableLocalization":false,"localizationFallbackLocale":"en","placeholders":{},"forceRedirect":false,"pagesPath":"./public","pagesEndpoint":"apps","customUrls":{},"customRoutes":[]} passwordPolicy: {"doNotAllowUsername":true,"resetTokenValidityDuration":10800} playgroundPath: /playground port: 1337 preserveFileName: false preventLoginWithUnverifiedEmail: false protectedFields: {"_User":{"":["email"],"role:admin":[]}} publicServerURL: http://localhost:1337 rateLimit: [] requestKeywordDenylist: [{"key":"_bsontype","value":"Code"},{"key":"constructor"},{"key":"proto"}] restAPIKey: asd78sd897df09as87dfh09asd revokeSessionOnPasswordReset: true scheduledPush: false security: {"enableCheck":false,"enableCheckLog":false} sessionLength: 31536000 startLiveQueryServer: true trustProxy: [] verbose: false verifyUserEmails: true jsonLogs: false level: undefined serverURL: http://localhost:1337/ state: initialized

`

VKlapan avatar Apr 06 '23 10:04 VKlapan

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

parse-github-assistant avatar Apr 06 '23 10:04 parse-github-assistant

Hi @VKlapan,

Parse GraphQL server follow the multi part graphql upload spec. You can't simply call a mutation into the Dashboard GraphQL console. Parse GraphQL server does not support base 64 file upload ( it's also a really bad practice since it do not leverage streaming, multi part http systems)

You can see some client side examples: https://github.com/jaydenseric/graphql-multipart-request-spec If you need to upload an image using the dashboard i can suggest you to just use the "Browser" section.

Or you can give a try to https://altairgraphql.dev/ that support multi part file upload.

Moumouls avatar Apr 08 '23 11:04 Moumouls

I'm closing this as it does not seem to be a Parse Server issue.

mtrezza avatar Apr 09 '23 20:04 mtrezza

Hi @VKlapan,

Parse GraphQL server follow the multi part graphql upload spec. You can't simply call a mutation into the Dashboard GraphQL console. Parse GraphQL server does not support base 64 file upload ( it's also a really bad practice since it do not leverage streaming, multi part http systems)

You can see some client side examples: https://github.com/jaydenseric/graphql-multipart-request-spec If you need to upload an image using the dashboard i can suggest you to just use the "Browser" section.

Or you can give a try to https://altairgraphql.dev/ that support multi part file upload.

Thanks. I'll try to use your tips.

VKlapan avatar Apr 10 '23 09:04 VKlapan

So... I've tried to use information from examples: https://github.com/jaydenseric/graphql-multipart-request-spec but with zero result (

For example, I have a question: Is this example correct and it has to work? https://github.com/jaydenseric/graphql-multipart-request-spec#curl-request

But I have error: {"error":"Unexpected token - in JSON at position 0"} Why?

VKlapan avatar Apr 10 '23 10:04 VKlapan

a few more details:

I have this error on my parse server 6.0.0 :

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Does anybody know how to fix it ?

VKlapan avatar Apr 18 '23 10:04 VKlapan

Hi @VKlapan we can't help here until you provide more details to reproduce the error, can you share a failing test or a reproduction repository.

I use the alpha release of Parse Server in production with file upload, my team has a lot of upload tests we don't have any issue.

Moumouls avatar Apr 18 '23 12:04 Moumouls

Hi @Moumouls I just try to learn Parse Server now so I don't have either any tests or reproduction repository. I have the simplest code to use base mutation createFile - createFile(input: CreateFileInput!): CreateFilePayload :

on web app

const [file, setFile] = useState<File | undefined>(undefined);

  const MUTATION = gql'
    mutation createFile($file: Upload!) {
      createFile(input: {upload: $file}) {
        fileInfo {
          name
          url
        }
      }
    }
  ';

  const [mutate] = useMutation(MUTATION);

  const onChangeHandler = (event: ChangeEvent<HTMLInputElement>) => {
    if (event.target && event.target.files) {
      const inputedFile = event.target.files[0];
      setFile(inputedFile);
    }
  };

  const onClickHandler = () => {
    console.log('onClick \n', file);
    mutate({variables: {file}});
  };

Payload on DevTools/Neywork: https://monosnap.com/direct/PjTfG1Uh4kwucPQhnFDVQc3k0Nj2Tm

response: {"error":"Unexpected token - in JSON at position 0"}

server log:

[47] parse-server running on http://localhost:1337/ [47] GraphQL running on http://localhost:1337/graphql Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:400:5) at ServerResponse.setHeader (node:_http_outgoing:663:11) at ServerResponse.header (/parse-server/node_modules/express/lib/response.js:794:10) at ServerResponse.send (/parse-server/node_modules/express/lib/response.js:174:12) at ServerResponse.json (/parse-server/node_modules/express/lib/response.js:278:15) at handleParseErrors (/parse-server/lib/middlewares.js:398:9) at Layer.handle_error (/parse-server/node_modules/express/lib/router/layer.js:71:5) at trim_prefix (/parse-server/node_modules/express/lib/router/index.js:326:13) at /parse-server/node_modules/express/lib/router/index.js:286:9 at Function.process_params (/parse-server/node_modules/express/lib/router/index.js:346:12) at next (/parse-server/node_modules/express/lib/router/index.js:280:10) at Layer.handle_error (/parse-server/node_modules/express/lib/router/layer.js:67:12) at trim_prefix (/parse-server/node_modules/express/lib/router/index.js:326:13) at /parse-server/node_modules/express/lib/router/index.js:286:9 at Function.process_params (/parse-server/node_modules/express/lib/router/index.js:346:12) at next (/parse-server/node_modules/express/lib/router/index.js:280:10)

VKlapan avatar Apr 19 '23 06:04 VKlapan

Hi @VKlapan , you have may be forgot to use a compatible upload client ?

Like: https://www.apollographql.com/docs/react/data/file-uploads/

Moumouls avatar Apr 19 '23 06:04 Moumouls

@Moumouls Hm... I think that I do it, don't I?: package.json:

"dependencies": { "@ant-design/icons": "^4.0.2", "@apollo/client": "^3.7.12", "@apollo/react-hooks": "^4.0.0", "@umijs/hooks": "^1.8.0", "antd": "^4.0.0", "apollo-upload-client": "^17.0.0", "graphql": "^16.6.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-infinite-scroller": "^1.2.4", "react-router-dom": "^6.10.0", "react-scripts": "5.0.1", "styled-components": "^5.0.1" },

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
});
client.defaultOptions = defaultOptions;

const Provider: React.FC<PropsWithChildren> = ({children}) => (
  <ApolloProvider client={client}>{children}</ApolloProvider>
);

export default Provider;

VKlapan avatar Apr 19 '23 06:04 VKlapan

Did you setup correctly the Apollo upload link ?

Moumouls avatar Apr 19 '23 06:04 Moumouls

@Moumouls

Did you setup correctly the Apollo upload link ?

I'm not sure(((( Can you give me some hints?

UPD: Is it correct?

import {createUploadLink} from 'apollo-upload-client';

const httpLink = createUploadLink({
  uri: GRAPHQL_URI,
});

VKlapan avatar Apr 19 '23 06:04 VKlapan

import { createUploadLink } from 'apollo-upload-client'
import {
	InMemoryCache,
	ApolloClient,
} from '@apollo/client'

const options = const options = {
			uri: graphQLUrl,
		}
const client = new ApolloClient({
				cache: new InMemoryCache(),
				link: createUploadLink(options),
			})

You client should be initialized like this @VKlapan

Moumouls avatar Apr 19 '23 06:04 Moumouls

I can be wrong, but I think that I have the same. (my full code as one piece):

import React, {PropsWithChildren} from 'react';
import {
  ApolloProvider,
  DefaultOptions,
  InMemoryCache,
  ApolloClient,
  createHttpLink,
} from '@apollo/client';
import {APPLICATION_ID, GRAPHQL_URI, API_KEY} from '../consts/env';
import {setContext} from '@apollo/client/link/context';

import {createUploadLink} from 'apollo-upload-client';

const httpLink = createUploadLink({
  uri: GRAPHQL_URI,
});

const authLink = setContext((_, {headers}) => {
  const token = localStorage.getItem('token');
  if (token) {
    return {
      headers: {
        'X-Parse-Application-Id': APPLICATION_ID,
        'X-Parse-REST-API-Key': API_KEY,
        'X-Parse-Session-Token': token,
      },
    };
  }
  return {
    headers: {
      'X-Parse-Application-Id': APPLICATION_ID,
      'X-Parse-REST-API-Key': API_KEY,
    },
  };
});

const defaultOptions: DefaultOptions = {
  watchQuery: {
    fetchPolicy: 'network-only',
  },
  query: {
    fetchPolicy: 'network-only',
  },
  mutate: {
    fetchPolicy: 'network-only',
  },
};

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
});
client.defaultOptions = defaultOptions;

console.log('CLIENT', authLink);

const Provider: React.FC<PropsWithChildren> = ({children}) => (
  <ApolloProvider client={client}>{children}</ApolloProvider>
);

export default Provider;

@Moumouls help me to find my bugs (((

VKlapan avatar Apr 19 '23 07:04 VKlapan

UPD one more attempt...

I use Altair GraphQL Client, which has embedded File upload mechanism and I try to use createFile mutation, which is embedded to Parse Server

and I receive the same Error:

{"error":"Unexpected token - in JSON at position 0"}

Can anybody explain to me - why? The base features don't work ! Who can prove the opposite?

What is very important - I use Parse Server 6.0.0

VKlapan avatar Apr 20 '23 17:04 VKlapan

Okay @VKlapan it's weird can you provide a reproduction repo with the failing behavior, for example just a simple react app with the apollo upload client configured and a guide to reproduce the error. I think it will be the only solution to understand the issue since parse server test suite is working correctly on this feature and also in my company we don't have any issue with this one ( we run on latest parse server version + some additional features from my fork)

Moumouls avatar Apr 20 '23 17:04 Moumouls

Reopened as the issue seems to require investigation

mtrezza avatar Apr 20 '23 22:04 mtrezza

Okay @VKlapan it's weird can you provide a reproduction repo with the failing behavior, for example just a simple react app with the apollo upload client configured and a guide to reproduce the error. I think it will be the only solution to understand the issue since parse server test suite is working correctly on this feature and also in my company we don't have any issue with this one ( we run on latest parse server version + some additional features from my fork)

Ok @Moumouls I'll create a repo. But I have to ask (to be sure that we understand each other correctly) - do you run parse server 6.0.0 in your company? or wich version?

VKlapan avatar Apr 21 '23 07:04 VKlapan

How is that issue related to this one?

mtrezza avatar Apr 27 '23 20:04 mtrezza