DynamoDBのテーブルが一致しないというエラー

AWS

Node.js18でDynamoDBをを使おうとしてました。

すると、こんなエラーが出続ける。

{“errorType”:”ValidationException”,”errorMessage”:”The provided key element does not match the schema”,”trace”:[“ValidationException: The provided key element does not match the schema”,” at throwDefaultError (/var/runtime/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)”,” at deserializeAws_json1_0GetItemCommandError (/var/runtime/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:1740:51)”,” at process.processTicksAndRejections (node:internal/process/task_queues:95:5)”,” at async /var/runtime/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24″,” at async /var/runtime/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/baseCommand/DynamoDBDocumentClientCommand.js:18:34″,” at async /var/runtime/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:13:20″,” at async StandardRetryStrategy.retry (/var/runtime/node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js:51:46)”,” at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:22″,” at async getExpireValue (file:///var/task/index.mjs:20:20)”,” at async Runtime.handler (file:///var/task/index.mjs:60:20)”]}

これ、DynamoDBのテーブルの内容とコードが一致しないというエラーなんですが、コピペで何度テーブル作り直しても出る。一致しないわけはない。

StackOverFlowに

import { DynamoDBClient, GetItemCommand, PutItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";

じゃなくて、

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand, PutCommand, UpdateCommand } from '@aws-sdk/lib-dynamodb';

を使うといいとあった。

なんでも、前者はデータの型をコードで指定しなければいけないけど、後者はそれをしなくて自動で判定してくれるからとある。

入れ替えてコード中の型指定も削除したら(ChatGPTにお任せ♡)このエラー出なくなりました。

それでも、元のコードはChatGPTが生成してエラーを指摘しても直らなかったものなんだよなぁ。

やっぱまだ100%ChatGPT頼りでプログラミングはできないわ。

どなたかのお役に立てたら。

コメント

タイトルとURLをコピーしました