首页 > 解决方案 > 无法读取未定义的属性“virtualMachines”

问题描述

我正在尝试构建一个将启动(除其他外)启动 Azure VM 的节点应用程序。

我目前可以毫无问题地登录 Azure,但是当我像下面 github 上的示例页面所示那样启动 VM 时,我得到了错误TypeError: Cannot read property 'virtualMachines' of undefined,我不确定为什么考虑到我实际上是从我找到的示例中获取代码。

https://github.com/Azure/azure-sdk-for-node/blob/master/examples/ARM/compute/vm-sample.js

'use strict';

// Packages
const cron = require("cron"),
    util = require('util'),
    path = require('path'),
    async = require('async'),
    Azure = require("azure"),
    msRestAzure = require('ms-rest-azure'),
    ComputeManagementClient = require('azure-arm-compute'),
    StorageManagementClient = require('azure-arm-storage'),
    NetworkManagementClient = require('azure-arm-network'),
    ResourceManagementClient = require('azure-arm-resource').ResourceManagementClient,
    SubscriptionManagementClient = require('azure-arm-resource').SubscriptionClient;

// Config
const config = require("./config.js");
var subscriptionId = config.azure_creds.AZURE_SUBSCRIPTION_ID;    
var AZURE_USER = config.azure_creds.AZURE_USER;    
var AZURE_PASS = config.azure_creds.AZURE_PASS;    


console.log('Starting application...');

msRestAzure.loginWithUsernamePassword(AZURE_USER, AZURE_PASS, (err, credentials) => {
  if (err) throw err;

    // let storageClient = Azure.createStorageManagementClient(credentials, subscriptionId);
    var computeClient;    
    var resourceGroupName = 'testing-resourceGroup';
    var vmName = 'vm-test-1';

console.log('Logged into Azure...');

console.log('Starting VM...');

computeClient.virtualMachines.start(resourceGroupName, vmName, function (err, result) {
    if (err) {
      console.log(util.format('\n???????Error while starting the VM:\n%s', 
        util.inspect(err, { depth: null })));
        console.log(err);
        throw(err);
    } else {
      console.log(util.format('\n######Start the VM is successful.\n%s', 
        util.inspect(result, { depth: null })));
      console.log(result);
    }
});

computeClient = new ComputeManagementClient(credentials, subscriptionId);
});

我希望该程序能够运行并启动我指定的 Azure VM,但它甚至无法正确运行该程序,让其启动 VM。以下是我运行程序时的输出

Starting application...
Logged into Azure...
Starting VM...
/home/ec2-user/environment/start-stop/app.js:43
computeClient.virtualMachines.start(resourceGroupName, vmName, function (err, result) {
             ^

TypeError: Cannot read property 'virtualMachines' of undefined
    at msRestAzure.loginWithUsernamePassword (/home/ec2-user/environment/start-stop/app.js:43:14)
    at /home/ec2-user/environment/start-stop/node_modules/ms-rest-azure/lib/login.js:361:14
    at /home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:473:16
    at next (/home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:5315:29)
    at /home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:958:16
    at /home/ec2-user/environment/start-stop/node_modules/ms-rest-azure/lib/login.js:121:5
    at /home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:473:16
    at iterateeCallback (/home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:976:17)
    at /home/ec2-user/environment/start-stop/node_modules/async/dist/async.js:958:16
    at /home/ec2-user/environment/start-stop/node_modules/ms-rest-azure/lib/login.js:118:14

标签: node.jsazureazure-sdk

解决方案


这就是您看到该错误的原因。这是您尝试执行的操作的非常简化的版本,以及您尝试执行的顺序:

// initialize a variable called computeClient which is undefined at this point
var computeClient; 

// Try to access the virtualMachines attribute of undefined. 
computeClient.virtualMachines; 

// Assign computeClient to a a new instance of an object that has a virtualMachines attribute
computeClient = new ComputeManagementClient(credentials, subscriptionId); 

删除所有干预代码后,很明显您正在尝试访问对象上的属性,然后才将变量分配给具有该属性的对象。我以前没有使用过这个库,但我认为你至少应该能够从这个错误中恢复,如果你这样做:

computeClient = new ComputeManagementClient(credentials, subscriptionId); 

你做之前:

computeClient.virtualMachines.start( ... )

推荐阅读