typescript 什么原因会导致类型脚本中的PostToConnectionCommand出现ERR_INVALID_ARG_TYPE

aoyhnmkz  于 2023-03-04  发布在  TypeScript
关注(0)|答案(1)|浏览(114)

我正在编写一个函数,它使用PostToConnectionCommand发送一个脚本形式的WebSocket消息。

async function sendMessageToClient(connectionId: string, payload) {
  try {
    console.log('Sending message to a connection', connectionId)

    const apiGatewayManagementApi = new ApiGatewayManagementApiClient({
      apiVersion: "2018-11-29",
      endpoint: `https://${apiId}.execute-api.us-east-1.amazonaws.com/${stage}`
    });

    console.log("apiId", apiId, "stage", stage, "payload", payload);
    

    const postToConnectionCommand = new PostToConnectionCommand({
      ConnectionId: connectionId,
      Data: payload
    });
    console.log("postToconnection command", postToConnectionCommand);
    

    const response = (await apiGatewayManagementApi.send(postToConnectionCommand));
    // Error is thrown at the line above
    console.log("response", response);
    

  } catch (err) {
    console.log('Failed to send message', JSON.stringify(err))
    if (err.statusCode === 410) {
      console.log('Stale connection')

      await deleteItem(connectionsTable, connectionId)
    }
  }

}

我正在尝试使用v3 SDK(aws-sdk/client-apigatewaymanagementapi)。我一直得到这个错误:

{
    "code": "ERR_INVALID_ARG_TYPE",
    "$metadata": {
        "attempts": 1,
        "totalRetryDelay": 0
    }
}

我相信 typescript 会捕捉到类型错误。有人知道如何修复这个问题吗?

pqwbnv8z

pqwbnv8z1#

问题来自node_modules。有效负载应为字符串。在.\node_modules\@aws-sdk\client-apigatewaymanagementapi\dist-types\models\models_0.d.ts中,为参数定义的两种类型都不是字符串,这会导致错误

相关问题