【问题标题】:How to use pageToken in options of compute.getVMs method using NodeJS?如何在使用 NodeJS 的 compute.getVMs 方法的选项中使用 pageToken?
【发布时间】:2021-07-02 09:01:07
【问题描述】:

我正在编写代码来列出在 Google Cloud Compute Engine 中创建的虚拟机。我正在使用 NodeJS 客户端库“@google-cloud/compute”Link to Client Library,然后我使用 compute.getVMs 方法来列出实例,并且我必须将 pageToken 作为 getVMs 方法的选项传递。我无法弄清楚如何将它作为代码中的选项传递并实现它。有人可以帮忙吗?

Link to compute.getVMs method and documentation

列出虚拟机实例的代码

'use strict';
async function main() {
  const Compute = require('@google-cloud/compute');
  const compute = new Compute();
  async function listVMs() {
    const vms = await compute.getVMs({
      maxResults: 10,
    });
    console.log(`VMs Present are `, vms);
  }
  listVMs();
}

【问题讨论】:

    标签: node.js google-cloud-platform pagination google-compute-engine


    【解决方案1】:

    如果我们查看在 getVMs() 调用中找到的示例代码,我们会发现我们能够批量读取 VM,而不是一次性获取所有 VM。这意味着我们可以“一次获得一个页面”。查看示例,结果(页面)准备好时调用的回调函数似乎是:

    • 错误
    • 虚拟机
    • 下一个查询
    • api响应

    当您收到此响应并且想要下一页结果页面时,您再次调用getVMs(),但这次将先前返回的nextQuery 作为输入参数传递给getVMs()。这一次,您将获得返回给您的下一页结果页面。

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2021-08-29
      • 2020-01-14
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多