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
null if no record is found. Task model, or null if no task with the provided id was found.null if no record is found. When you use await with , it should return the instance of the model or null, not a Promise.markTaskAsDonefindUniquefindUniquefindUniquefindUniquetaskJSON.stringify()JSON.stringify()JSON.parse()JSON.parse()JSON.parse()JSON.stringify(post)setStatesetStatesetStatepostJsonpostJsonpostJsonpostpostcommentscommentscommentscommentspostObjectimport { 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