🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with sending comments to Sanity studio from deployed version

13 replies
Last updated: Sep 7, 2022
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:

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)
}
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:

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);
    }
so how can I'll be able to send comments from deployed version of my website?

.
Sep 7, 2022, 10:17 AM
Where do you deploy? Vercel?
Sep 7, 2022, 12:04 PM
Yes in vercel
Sep 7, 2022, 1:05 PM
Did you define your token in Vercel environment variables settings?
Sep 7, 2022, 1:06 PM
Yes
Sep 7, 2022, 1:08 PM
Would be good to have some logs.
Sep 7, 2022, 1:10 PM
Where can I find those logs? I am new in this
Sep 7, 2022, 1:12 PM
it would be great if you helped
Sep 7, 2022, 1:22 PM
here are they
Sep 7, 2022, 1:32 PM
Just to comfirm comment token should have 'Editors' access right?
Sep 7, 2022, 1:36 PM
Read/write, so I guess that’s Editor yes.
Sep 7, 2022, 1:51 PM
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
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!
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.

Was this answer helpful?