这个问题通常是因为您的运行环境(例如Node.js)不支持URL构造函数所导致的。要解决这个问题,您可以使用一个URL解析库,例如url-parse,来代替URL构造函数。
代码示例:
const AWS = require('aws-sdk'); const URLParse = require('url-parse');
AWS.config.update({ region: 'us-west-2', credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: '...' }) });
const endpoint = new URLParse('https://my-endpoint.com'); const client = new AWS.ApiGatewayManagementApi({ endpoint: endpoint.href, apiVersion: '2018-11-29' });
client.postToConnection({ ConnectionId: '...', Data: 'Hello world!' }, (err, data) => { if (err) { console.log(err); } else { console.log('Message sent:', data); } });