W
Wasp-lang2mo ago
Karan

TypeError: Cannot read properties of undefined (reading 'id')

I am having issues with creating an entity using nested relations in Prisma. I don't think this is a wasp issue but I wanted to ask to make sure. The only thing I am wondering about is in case I am missing some setup in the main.wasp file. Happening in file src/actions.js on https://github.com/ksk385/c2log-app
4 Replies
kapa.ai
kapa.ai2mo ago
Hi @Karan. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but I can't provide a specific solution to your problem as the provided knowledge sources don't contain the exact information about the issue you're facing. However, I can give you some general advice based on the information available. In Wasp, when you're creating an entity using Prisma, you need to make sure that the entity is properly defined in your main.wasp file and that it's included in the entities array of the action that's using it. Here's an example from the Wasp documentation (https://wasp-lang.dev/docs/data-model/operations/actions#using-entities-in-actions):
action createTask {
fn: import { createTask } from "@src/actions.js",
entities: [Task]
}
action createTask {
fn: import { createTask } from "@src/actions.js",
entities: [Task]
}
In your action function, you can then access the entity through the context argument:
export const createTask = async (args, context) => {
const newTask = await context.entities.Task.create({
data: {
description: args.description,
isDone: false,
},
})
return newTask
}
export const createTask = async (args, context) => {
const newTask = await context.entities.Task.create({
data: {
description: args.description,
isDone: false,
},
})
return newTask
}
If you're getting a TypeError: Cannot read properties of undefined (reading 'id'), it might be because you're trying to access a property of an object that hasn't been initialized. Make sure that the object you're trying to access and its id property exist before you try to use them. If you continue to have issues, I would recommend checking the Wasp documentation (https://wasp-lang.dev/docs) and the Prisma documentation (https://www.prisma.io/docs/) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Karan, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos2mo ago
There is nothing special you need to set in main.wasp! It sounds to me like your args.userId is maybe not defined. Might be due to how you are calling that action? Have you tried logging args in the action to make sure they look like what you expect them to look like?
Karan
Karan2mo ago
Figured it out - the problem was that I was not awaiting for the promise to resolve
martinsos
martinsos2mo ago
Ah makes sense, I completely missed it!