https://serverless-stack.com/ logo
#help
Title
# help
s

Schuyler Sloane

05/14/2022, 8:16 PM
Is it just me or is there a mistake in the Rest-api-example? I keep getting stubborn type errors on get.ts and update.ts and my code is exactly the same as what is presented on the page
t

thdxr

05/14/2022, 8:43 PM
it's possible - what are you seeing?
s

Schuyler Sloane

05/14/2022, 8:47 PM
Sorry if there is a stupid solution by the way. Its been a while since I've used typescript
t

thdxr

05/14/2022, 8:50 PM
gotcha this example is missing something, let me try to fix
it should be written like this
Copy code
import notes from "./notes";
import { APIGatewayProxyHandlerV2 } from "aws-lambda"

export const main: APIGatewayProxyHandlerV2 = async (event) => {
  const note = notes[event.pathParameters.id];
  return note
    ? {
        statusCode: 200,
        body: JSON.stringify(note),
      }
    : {
        statusCode: 404,
        body: JSON.stringify({ error: true }),
      };
}
s

Schuyler Sloane

05/14/2022, 8:54 PM
I tried something like that earlier and still had this error:
t

thdxr

05/14/2022, 8:56 PM
gotcha so now the error is technically ts doesn't know that
pathParameters.id
always exists. You can assert that with
event.pathParameters.id!
it doesn't know it doesn't always exist because ts isn't aware it's always mounted to a path that has that value - could technically mount it to a path that does not
a "safer" way would be to do
if (!event.pathParameters.id) throw new Error()
at the top and then ts won't complain
s

Schuyler Sloane

05/14/2022, 8:59 PM
I'm trying both those and it still says "object is possibly undefined" along with the previous errors
t

thdxr

05/14/2022, 9:00 PM
oh sorry you probably also need
event.pathParameters?.id
since
pathParameters
itself could be undefined
you might get one more error still
let me clone this example properly so I can see
s

Schuyler Sloane

05/14/2022, 9:02 PM
"type undefined cannot be used as an index type" even with the nullable option
t

thdxr

05/14/2022, 9:04 PM
how did you start this project?
s

Schuyler Sloane

05/14/2022, 9:05 PM
Pretty much exactly what the tutorial said to do.
t

thdxr

05/14/2022, 9:06 PM
which tutorial? we're in the process of migrating a lot of our content to our 1.0 changes
t

thdxr

05/14/2022, 9:12 PM
ok I reproduced your error one sec
the following code works
Copy code
import notes from "./notes";
import { APIGatewayProxyHandlerV2 } from "aws-lambda";

export const main: APIGatewayProxyHandlerV2 = async (event) => {
  const note = notes[event.pathParameters?.id!];
  return note
    ? {
        statusCode: 200,
        body: JSON.stringify(note),
      }
    : {
        statusCode: 404,
        body: JSON.stringify({ error: true }),
      };
};
Copy code
export default {
  id1: {
    noteId: "id1",
    userId: "user1",
    createdAt: Date.now(),
    content: "Hello World!",
  },
  id2: {
    noteId: "id2",
    userId: "user2",
    createdAt: Date.now() - 10000,
    content: "Hello Old World! Old note.",
  },
} as Record<
  string,
  {
    noteId: string;
    userId: string;
    createdAt: number;
    content: string;
  }
>;
We need to update this tutorial - it's not the greatest example because you'd never really hardcode data like this which is causing TS to think there can never be anything in it besides
id1
and
id2
s

Schuyler Sloane

05/14/2022, 9:17 PM
Yup that did it. I thought there had to be something screwy about that notes file. I really appreciate it.