我正在尝试构建一个节点应用程序,该应用程序将(其中包括)启动Azure VM。
我目前可以正常登录Azure,但是当我启动虚拟机(如在github上的以下示例页中所示)时,出现错误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
答案 0 :(得分:1)
这就是为什么您看到该错误的原因。这是您尝试做的事情的简化版本,以及您尝试做的顺序:
// 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( ... )