getting-started-nextjs
getting-started-nextjs copied to clipboard
Error
I am following the repo code. This is my frontend code:
const replicatePrompt = `Create a prompt for stable diffusion to create an image based on the user's entry. User's entry is : User Entry: ${express}`
const handleReplicate = async () => {
const response = await fetch("/api/predictions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: replicatePrompt,
}),
})
let prediction: any = await response.json()
if (response.status !== 201) {
setError(prediction.detail)
return
}
setPrediction(prediction)
while (
prediction.status !== "succeeded" &&
prediction.status !== "failed"
) {
await sleep(1000)
const response = await fetch("/api/predictions/" + prediction.id)
prediction = await response.json()
if (response.status !== 200) {
setError(prediction.detail)
return
}
console.log({ prediction })
setPrediction(prediction)
}
}
This is the api file code extract:
const { prompt } = req.body.prompt
const prediction = await replicate.predictions.create({
version: "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
input: {
prompt: prompt,
negative_prompt: "text, meat",
},
})
When i run the above code, prompt field is empty in the final api call.
If i use const { prompt } = await req.json() then i get not json error.
What is the error in the above code? How can i solve this?