0

When I try to send the slack notification locally, it works fine, but when I tried it in production I get this typical CORS error. "Access to fetch at 'https:///trpc/orientation.create?batch=1' from origin 'https://' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

this is my TRPC router:

    async resolve({ input: { data } }) {
      try {
        const newOrientation = await prisma.client.$transaction(
          async client => {
            const orientation = await transactionCreateOrientation(client, data)
            await transactionCreateProducts(
              Number(orientation.id),
              data.products,
            )
            return orientation
          },
        )

        //send slack notification
        if (process.env.APP_ENV === 'production') {
          //find brand
          const brand = await prisma.client.brand.findUnique({
            where: {
              id: newOrientation.brandId,
            },
          })

          await slackNotify(
            Number(newOrientation.id),
            newOrientation.campaignName,
            brand?.name ?? '-',
            `myorigin/app/orientation/${newOrientation.id}`,
          )
        }

        return newOrientation

and this is the slackNotify Function:

import axios from 'axios'
import { Logger } from 'server-common'

const slackToken = process.env.SLACK_TOKEN
const url = process.env.SLACK_POST_MESSAGE_URL

export async function slackNotify(
  id: number,
  campaignName: string,
  brandName: string,
  link: string,
) {
  try {
    if (!url) return
    await axios.post(
      url,
      {
        channel: '#test',
        blocks: [],
          },
        ],
      },
      // { headers: { authorization: `Bearer ${slackToken}` } },
    )
    Logger.dir('Slack notification sent successfully!')
  } catch (err) {
    throw new Error('Error sending notification' + err)
  }
}
Arpit
  • 467
  • 1
  • 8
  • 18
  • https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header – derpirscher Mar 28 '23 at 06:58

0 Answers0