Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

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?