✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Uploading image to Sanity from Next.js API with server-side node.js

10 replies
Last updated: Apr 8, 2020
Trying to upload an image to Sanity from Next.js /api. What am I doing wrong? 😓
TypeScript doesn't complain about the type, but the error message says it's wrong type.
Apr 8, 2020, 10:05 AM
is this client side or server side?
Apr 8, 2020, 10:31 AM
Server side
Apr 8, 2020, 10:31 AM
This is next.js, and I'm pretty sure the
/pages/api/
will result in a server side node.js thing.
Apr 8, 2020, 10:32 AM
what fetch polyfill/library are you using?
Apr 8, 2020, 10:33 AM
isomorphic-unfetch
Apr 8, 2020, 10:33 AM
I don't think our node client can handle Blobs, any way to convert it to a buffer?
Apr 8, 2020, 10:34 AM
try
r.buffer()
instead of
r.blob()
Apr 8, 2020, 10:37 AM
Ahh, that works! Typescript didn't know about
r.buffer()
only
r.arrayBuffer()
which didn't work.
Apr 8, 2020, 10:38 AM
//@ts-ignore
to the rescue 😉
Apr 8, 2020, 10:39 AM
Hehe 😅 I just switched to
node-fetch
since this always will be server side, and now everything types correctly as well 🥳
Apr 8, 2020, 10:41 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?