Issue with sending comments to Sanity studio from deployed version
13 replies
Last updated: Sep 7, 2022
O
I created comment system with sanity and next js with the help from this tutorial on youtube from sanity youtbue channel.Its working perfect in localhost. But on my deployed version its not sending the comments to sanity studio. (Rest of content from sanity is being fetched).
here is comment.js from pages/api/comment.js:
Token I am using has Editor access as you can see in the image.
Here is the code for onClick event of submiting the comment:
so how can I'll be able to send comments from deployed version of my website?
.
here is comment.js from pages/api/comment.js:
export default async function comment(req, res) { const { name, text, email, _id } = JSON.parse(req.body) client.config({ token: process.env.SANITY_API_TOKEN_COMMENT }).create({ _type: 'comment', name, comment: text, email, post: { _type:'reference', _ref: _id } }) res.status(200) }
Here is the code for onClick event of submiting the comment:
const onSubmit = async (data) => { setIsSubmitting(true); try { await fetch("/api/comment", { method: "POST", body: JSON.stringify({ ...data, _id }), }); setIsSubmitting(false); setHasSubmitted(true); setTimeout(()=>{ setHasSubmitted(false) },3000) if(getValues('storeData')){ window.localStorage.setItem('name',getValues('name')) window.localStorage.setItem('email',getValues('email')) } } catch (err) { console.log(err); }
.
Sep 7, 2022, 10:17 AM
K
Where do you deploy? Vercel?
Sep 7, 2022, 12:04 PM
S
Yes in vercel
Sep 7, 2022, 1:05 PM
K
Did you define your token in Vercel environment variables settings?
Sep 7, 2022, 1:06 PM
S
Yes
Sep 7, 2022, 1:08 PM
K
Would be good to have some logs.
Sep 7, 2022, 1:10 PM
S
Where can I find those logs? I am new in this
Sep 7, 2022, 1:12 PM
O
it would be great if you helped
Sep 7, 2022, 1:22 PM
K
Sep 7, 2022, 1:26 PM
O
here are they
Sep 7, 2022, 1:32 PM
O
Just to comfirm comment token should have 'Editors' access right?
Sep 7, 2022, 1:36 PM
K
Read/write, so I guess that’s Editor yes.
Sep 7, 2022, 1:51 PM
K
For the logs, I meant putting some logs in your function to understand what’s happening on Vercel. So we know if the request to Sanity fails and why.
Sep 7, 2022, 1:52 PM
O
After rechecking my entire code I just found out that I didn't used await for async function inside /api/comment.js. And now its been working.
Thanks for help!
Thanks for help!
Sep 7, 2022, 2:40 PM
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.