question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File uploads but getting undefined JSON error

See original GitHub issue

Hi Jayden, thanks for making this awesome package! I’m running into this weird issue, my actual file gets uploaded, but I get this error:

_SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at form.parse (/Users/vlady/WebProjects/yupty-api/node_modules/apollo-upload-server/src/index.js:20:31) at IncomingForm.<anonymous> (/Users/vlady/WebProjects/yupty-api/node_modules/formidable/lib/incoming_form.js:102:9) at emitOne (events.js:96:13) at IncomingForm.emit (events.js:188:7) at IncomingForm._error (/Users/vlady/WebProjects/yupty-api/node_modules/formidable/lib/incoming_form.js:290:8) at IncomingMessage.<anonymous> (/Users/vlady/WebProjects/yupty-api/node_modules/formidable/lib/incoming_form.js:120:12) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at abortIncoming (_http_server.js:284:11) at Socket.serverSocketCloseListener (_http_server.js:297:5) at emitOne (events.js:101:20) at Socket.emit (events.js:188:7) at TCP._handle.close [as onclose] (net.js:501:12)

My server looks like this:

app.use('/graphql',
	authenticate,
	bodyParser.json(),
	// Uploads
	apolloUploadExpress({
		// Optional, defaults to OS temp directory
		uploadDir: './uploads',
	}),
	graphqlExpress(req => {
		const query = req.query.query || req.body.query;
		if (query && query.length > 2000) {
			// Probably indicates someone trying to send an overly expensive query
			throw new Error('Query too large.');
		}

		return {
			schema: makeExecutableSchema({
				typeDefs: schema,
				resolvers,
			}),
			context: {
				user: req.user,
				token: req.headers.authorization,
			},
		};
	}),
);

I’ve been trying to fix and I have no idea what’s going on…

The query on the client looks like this:


const CreateEventFormWithMutation = graphql(createEventMutation, {
	props: ({ ownProps, mutate }) => ({
		// This function will be available as a prop in the form:
		createEvent: ({
			venue,
			start_time,
			description,
			picture,
		}, artistIDs, genreIDs) => {
			const variables = {
				venue_id: venue.id,
				start_time,
				description,
				artistIDs,
				genreIDs,
			};

			// Add picture to variables if there is one
			if (Object.getOwnPropertyNames(picture).length > 0) {
				variables.picture = picture;
			}

			return mutate({
				variables,
				// Update event feed with data from form, no need for network request
				optimisticResponse: {
					__typename: 'Mutation',
					createEvent: {
						__typename: 'Event',
						// Note that we can access the props of the container at `ownProps`
						venue,
						start_time,
						artists: ownProps.newEvent.artists,
						description,
					},
				},
				// Name the query to update, in this case: the query from EventFeedContainer, "getEvents"
				updateQueries: {
					getEvents: (prev, { mutationResult }) => {
						return update(prev, {
							events: {
								$unshift: [mutationResult.data.createEvent],
							},
						});
					},
				},
			});
		},
	}),
})(CreateEventForm);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jaydensericcommented, Mar 26, 2017

Awesome. If you discover a bug I’ll reopen. It would be good to know what it was; we might be able to add a more obvious error message if it’s a common issue.

1reaction
jaydensericcommented, Mar 26, 2017

I think the error is that fields.variables is undefined here when attempting to decode the GraphQL variables.

Not sure why that would be. I would suggest working backwards from there to get to the bottom of it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

upload image from mobile to server and shows JSON is ...
I have req like to upload image from mobile to server. First I am converting the image to base64 and send that data...
Read more >
Re: JSON is undefined error - Adobe Support Community
I've use JSON.parse() and JSON.stringify() numerous times. For some very odd reason, whenever I try to use JSON now, an error is thrown...
Read more >
Error while trying to upload a picture. - WordPress.org
I encountered an error that doesn't allow me to upload the image. That's the error: Updating failed. Error message: The response is not...
Read more >
error syntaxerror: "undefined" is not valid json - You.com
i learning react and the error "Uncaught SyntaxError: "undefined" is not valid JSON" appeared. this is the part of the code that involves...
Read more >
JSON is undefined error from UI Extensibility Apps
This particular error indicates that the JSON object the browser should provide does not exist, which can happen with browsers like Internet ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found