W
Wasp•5mo ago
dtschannen

Query an entity throws "unexpected token: entities" Error

Hi everyone. I try to query an entity as follow: Wasp File: query findQRCodeById { fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById" entities: [QRCode] } And my findQRCodeById Typescript file: import { QRCode } from 'wasp/entities' import { type FindQRCodeById } from 'wasp/server/operations' type GetQRCodeByIdArgs = { id: string } export const getQRCodeById: FindQRCodeById<GetQRCodeByIdArgs, QRCode | null> = async (args, context) => { return context.entities.QRCode.findUnique({ where: { id: args.id }, }) } It gives me this Error: 4 errors found: - /home/dtschannen/projects/qrftw/app/main.wasp @ 204:3-10 Parse error: Unexpected token: entities Expected one of the following tokens instead: '}',',' 203 | fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById" 204 | entities: [QRCode] 205 | } - /home/dtschannen/projects/qrftw/app/main.wasp @ 204:11 Parse error: Unexpected token: : Expected one of the following tokens instead: <identifier> 203 | fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById" 204 | entities: [QRCode] 205 | } - /home/dtschannen/projects/qrftw/app/main.wasp @ 204:13 Parse error: Unexpected token: [ Expected one of the following tokens instead: <identifier>,<eof> 203 | fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById" 204 | entities: [QRCode] 205 | } - /home/dtschannen/projects/qrftw/app/main.wasp @ 204:14-19 Parse error: Unexpected token: QRCode Expected one of the following tokens instead: <eof> 203 | fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById" 204 | entities: [QRCode] 205 | }
5 Replies
kapa.ai
kapa.ai•5mo ago
Hi @dtschannen. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided, it seems like there might be a syntax error in your Wasp query. The entities field in your query should be a list of entities you wish to use inside your Job. Here is the correct syntax for using entities in a query:
query findQRCodeById {
fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById"
entities: ['QRCode']
}
query findQRCodeById {
fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById"
entities: ['QRCode']
}
Please note that the entities should be provided as a list of strings. If you continue to face issues, it might be due to the semantics of the entities field. There was a Github issue (https://github.com/wasp-lang/wasp/issues/967) reported where the semantics of the entities field were not intuitive and caused Wasp to break with an error. This issue has been closed, indicating that it has been resolved. If the problem persists, I would recommend checking the version of Wasp you are using and updating it if necessary. If the issue still remains, consider reporting it on the Wasp Github page. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @dtschannen, 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?
dtschannen
dtschannenOP•5mo ago
@kapa.ai I dont think the issue the Wasp file is exactly like many examples i have seen. Im assuming the issue is maybe in the server component file? Whats the right way to formulate the query?
kapa.ai
kapa.ai•5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Based on the information provided, your server-side query implementation seems correct. However, there might be a misunderstanding in how you're declaring the query in the Wasp file. In Wasp, you declare queries in the .wasp file and implement them using NodeJS. Here is the correct way to declare a query:
query findQRCodeById {
fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById.js"
}
query findQRCodeById {
fn: import { findQRCodeById } from "@src/qr-generator/findQRCodeById.js"
}
In this declaration, findQRCodeById is the name of the query, and the fn field points to the NodeJS function that implements the query. The entities field is not used in the query declaration. The NodeJS function should be implemented as follows:
import { QRCode } from 'wasp/entities'

type GetQRCodeByIdArgs = {
id: string
}

export const findQRCodeById = async (args: GetQRCodeByIdArgs, context: any): Promise<QRCode | null> => {
return context.entities.QRCode.findUnique({
where: { id: args.id },
})
}
import { QRCode } from 'wasp/entities'

type GetQRCodeByIdArgs = {
id: string
}

export const findQRCodeById = async (args: GetQRCodeByIdArgs, context: any): Promise<QRCode | null> => {
return context.entities.QRCode.findUnique({
where: { id: args.id },
})
}
In this function, args is an object containing the arguments passed to the query, and context is an object that provides access to the database and authentication information. You can then use the query in your code by importing it from wasp/client/operations:
import { findQRCodeById } from 'wasp/client/operations'

// ...

const qrCode = await findQRCodeById({ id: 'some-id' })
import { findQRCodeById } from 'wasp/client/operations'

// ...

const qrCode = await findQRCodeById({ id: 'some-id' })
For more information, you can refer to the Wasp documentation on queries (https://wasp-lang.dev/docs/data-model/operations/queries#working-with-queries). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @dtschannen, 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?
miho
miho•5mo ago
I believe you are missing a comma 😊 after the fn line
query getTasks {
fn: import { getTasks } from "@src/queries",
entities: [Task]
}
query getTasks {
fn: import { getTasks } from "@src/queries",
entities: [Task]
}
dtschannen
dtschannenOP•5mo ago
@miho Man how could i miss that.. it costed me like 2 hours already 🤓 Thank you very much!!
Want results from more Discord servers?
Add your server