Search code examples
node.jsazureazure-sdk

Cannot read property 'virtualMachines' of undefined


I am trying to build a node application that will start (among other things) start a Azure VM.

I can currently log into Azure without issue but when I go to start the VM like seen in the below example page on github I get the error TypeError: Cannot read property 'virtualMachines' of undefined and I am unsure of why considering I practically took the code from the example I found.

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);
});

I expected the program to run and start an Azure VM that I specified but it will not even run the program properly let along start the VM. Below is my output when I run my program

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

Solution

  • Here is why you are seeing that error. Here is a very simplified version of what you are trying to do, and the order you are trying to do it in:

    // 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); 
    

    When all the intervening code is removed it is clear that you are trying to access an attribute on the object, before you have assigned your variable to an object that has that attribute. I have not used this library before, but I think you should be able to recover from this error at least if you do:

    computeClient = new ComputeManagementClient(credentials, subscriptionId); 
    

    before you do:

    computeClient.virtualMachines.start( ... )