sample code for reading a document from db and parsing it to json from the action
sample code for reading a document from db and parsing it to json from the action
findUnique function from Prisma, the return type is a Promise that resolves to the instance of the model you're querying for, or null if no record is found. task would be an instance of the Task model, or null if no task with the provided id was found.JSON.parse():JSON.parse() functions are used to convert between JSON and JavaScript objects.setState), but the types don't match.findUnique function returns a Promise that resolves to the instance of the model you're querying for, or null if no record is found. When you use await with findUnique, it should return the instance of the model or null, not a Promise.setState), you'll need to parse it back into an object with JSON.parse():setState.findUnique to make sure they match.markTaskAsDoneJSON.stringify()JSON.stringify()JSON.stringify(post)postJsonpostJsonpostJsonpostpostcommentscommentscommentscommentspostObjectimport { type CreateTask, type MarkTaskAsDone } from 'wasp/server/operations'
type Task = {
id: number
description: string
isDone: boolean
}
// our "database"
let nextId = 4
const tasks = [
{ id: 1, description: 'Buy some eggs', isDone: true },
{ id: 2, description: 'Make an omelette', isDone: false },
{ id: 3, description: 'Eat breakfast', isDone: false },
]
// The 'args' object is something sent by the caller (most often from the client)
export const markTaskAsDone: MarkTaskAsDone<Pick<Task, 'id'>, void> = (
args
) => {
const task = tasks.find((task) => task.id === args.id)
if (!task) {
// We'll show how to properly handle such errors later
return
}
task.isDone = true
}const task = await Task.findUnique({ where: { id: args.id } })const taskJson = JSON.stringify(task)const taskObject = JSON.parse(taskJson)const post = await context.entities.PostComments.findUnique({
where: { postId: args.postId }
})
if (post) {
const postJson = JSON.stringify(post)
// Use postJson here
} else {
// Handle the case where no post was found
}setState(JSON.parse(postJson))const post = await context.entities.PostComments.findUnique({
where: { postId: args.postId }
})
if (post) {
const comments = post.comments
// Now you can work with `comments` array
} else {
// Handle the case where no post was found
}const postJson = JSON.stringify(post)
const postObject = JSON.parse(postJson)
const comments = postObject.comments