无法读取未定义的 属性 'virtualMachines'
Cannot read property 'virtualMachines' of undefined
我正在尝试构建一个将启动(除其他外)启动 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
这就是您看到该错误的原因。这是您尝试执行的操作的非常简化的版本,以及您尝试执行的顺序:
// 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( ... )
我正在尝试构建一个将启动(除其他外)启动 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
这就是您看到该错误的原因。这是您尝试执行的操作的非常简化的版本,以及您尝试执行的顺序:
// 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( ... )