Is it just me or is there a mistake in the Rest-ap...
# help
s
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
it's possible - what are you seeing?
s
Sorry if there is a stupid solution by the way. Its been a while since I've used typescript
t
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
I tried something like that earlier and still had this error:
t
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
I'm trying both those and it still says "object is possibly undefined" along with the previous errors
t
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
"type undefined cannot be used as an index type" even with the nullable option
t
how did you start this project?
s
Pretty much exactly what the tutorial said to do.
t
which tutorial? we're in the process of migrating a lot of our content to our 1.0 changes
t
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
Yup that did it. I thought there had to be something screwy about that notes file. I really appreciate it.