Nexus-prisma error: Property 'title' does not exist on type '{}'


#1

I hope this is the correct place to post this.
For the life of me I cannot get the simple example to add a computed field to compile. On page https://nexus.js.org/docs/database-access-with-prisma, step 6 “add computer fields to a model” I always get an error. This is the instructions:

const Post = prismaObjectType({
  name: "Post",
  definition(t) {
    t.string("uppercaseTitle", {
      resolve: ({ title }, args, ctx) => title.toUpperCase(),
    });
  },
});

VSCode always gives the error in my title. Trying to run it shows: ReferenceError: title is not defined. I don’t understand where it is trying to get “title”. I see no context. Any help pointing me in the right direction would be appreciated.


#2

He Jerry, I’m pretty new to Nexus but what I think is happening is that you don’t have title declared on your PrismaObjectType.
Inside the definition you can declare the fields you want to show. But in your example you’re trying to compute tittle to uppercase, but you haven’t declared title yet. So you’re trying to get title from the parent, but it’s not there.

Try:

const Post = prismaObjectType({
  name: "Post",
  definition(t) {
    t.prismaFields(["*"]); //makes all fields accessible
    t.string("uppercaseTitle", {
      resolve: ({ title }, args, ctx) => title.toUpperCase()
    });
  }
});

This solved the error in my VSCode.


#3

My bad. I wasn’t using the actual field “title” and I forgot to run the nexus-prisma-generate command again, which solved my problem.

Thanks. I had already added the prismaFields line.


#4

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.