import { Restaurant } from "@/types"
import { useAuth0 } from "@auth0/auth0-react"
import { useMutation } from "@tanstack/react-query"
import { toast } from "sonner"
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
export const useCreateMyRestaurant = () => {
const{ getAccessTokenSilently} = useAuth0()
const createMyRestaurantRequest = async(restaurantFormData:FormData):
Promise<Restaurant> => {
const accessToken = await getAccessTokenSilently()
const response = await fetch(`${API_BASE_URL}/api/my/restaurant`,{
method: 'POST',
headers:{
Authorization: `Bearer ${accessToken}`,
},
body:restaurantFormData,
})
console.error(response)
if (!response.ok){
throw new Error("Failed to create restaurant")
}
return response.json()
}
const {
mutateAsync:createRestaurant,
isPending,
isSuccess,
error,
} = useMutation({mutationFn:createMyRestaurantRequest})
if(isSuccess){
toast.success("Restaurant Created")
}
if(error){
toast.error("Unable to update restaurant")
}
return{createRestaurant,isPending}
}
import {Request, Response} from "express"
import Restaurant from "../models/restaurant"
import cloudinary from "cloudinary"
import mongoose from "mongoose"
const getMyRestaurant = async ()=> {}
const createMyRestaurant = async(req:Request, res:Response) =>{
try {
const existingRestaurant = await Restaurant.findOne({user:req.userId})
if(existingRestaurant){
return res.
status(409)
.json({message:"User restaurant already exist"})
}
const image = req.file as Express.Multer.File
const base64Image = Buffer.from(image.buffer).toString("base64")
const dataURI = `data:${image.mimetype};base64,${base64Image}`
const uploadResponse = await cloudinary.v2.uploader.upload(dataURI)
const restaurant = new Restaurant(req.body)
restaurant.imageUrl = uploadResponse.url
restaurant.user = new mongoose.Types.ObjectId(req.userId)
restaurant.lastUpdated = new Date()
await restaurant.save()
res.status(201).json({message:"Restaurant created successfully"})
} catch (error) {
console.log(error)
res.status(500).json({message:"Something went Wrong"})
}
}
export default {
createMyRestaurant
}
in postman its run smoothly but after i make a frontend the error is
Request Method: POST Status Code: 400 Bad Request Referrer Policy: strict-origin-when-cross-origin
Request URL:
http://localhost:7000/api/my/restaurant
Request Method:
POST
Status Code:
400 Bad Request
Referrer Policy:
strict-origin-when-cross-origin