typescript: listServiceLevelObjectives gives UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT

typescript: listServiceLevelObjectives gives UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT

我正在尝试使用打字稿通过 REST API 获取 google 云项目中的 SLO 列表,但收到错误。请问你能帮帮我吗。错误是:

(node:47173) UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: Received invalid service name: [object Object] at Object.callErrorFromStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call.ts:81:24) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client.ts:334:36) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client-interceptors.ts:434:34) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client-interceptors.ts:397:48) at Http2CallStream.outputStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:230:22) at Http2CallStream.maybeOutputStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:280:14) at Http2CallStream.endCall (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:263:12) at Http2CallStream.handleTrailers (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:412:10) at ClientHttp2Stream.emit (events.js:314:20) at emit (internal/http2/core.js:291:8) at processTicksAndRejections (internal/process/task_queues.js:83:22) at emitUnhandledRejectionWarning (internal/process/promises.js:168:15) at processPromiseRejections (internal/process/promises.js:247:11) at processTicksAndRejections (internal/process/task_queues.js:94:32)

老实说,我是 Typescript 的新手,通常我使用 Java,所以可能是一个愚蠢而简单的错误。

我的代码是: list_slos.ts 在源目录

export module ListSloTest {
    export async function listSlos(projectId: string) {
        // Imports the Google Cloud client library
        const monitoring = require('@google-cloud/monitoring');

        // Creates a client
        const client = new monitoring.ServiceMonitoringServiceClient();

        const request = {
            parent: client.projectPath(projectId),
        };
        console.log(projectId)

        const [slos] = await client.listServiceLevelObjectives({parent: request});

        slos.forEach((slo: any) => {
            console.log('');
            console.log('slo: %j', slo);
        });
**list_slos.ts in test directory**
import "mocha";
import * as assert from "assert";
import {ListSloTest} from "../dist/list_slos";

describe("list_slos",()=>{

    before(function () {
        process.env = {GOOGLE_CLOUD_PROJECT: 'projectId',
            GOOGLE_APPLICATION_CREDENTIALS: '/somePath/projectId-a17ce3d7c2e6.json'};
    });

    it("should list slos in given project",()=>{

        ListSloTest.listSlos("projectId");
        //console.log("hello, world!")
        assert.ok(true);

    });

});

我正在将源代码编译到 dist 目录,然后在测试中使用它。

已解决。请求应该是下一个:

const request = client.projectServicePath(projectId, serviceName);

这适用于@google-cloud/monitoring 的 2.1.1 版 好可惜,没找到例子,只针对1.7版本,2.1.1不行