Skip to content Skip to sidebar Skip to footer

Chicken And Egg Situation With Retrieve 8-digit Codes From New Created Accounts

I am refactoring a script that creates new user accounts on google script G-Suite API. The script works fine, and prepared an email templates, with login credentials--all as expect

Solution 1:

Answer :

Verification codes need to be generated first for every created user. Since this is a script, you can use AdminDirectory.VerificationCodes.generate to do this before listing.

Sample:

function generateKeys() {
  AdminDirectory.VerificationCodes.generate("dummyemail@dummy.com");
  var keys = AdminDirectory.VerificationCodes.list("dummyemail@dummy.com").items[0].verificationCode;
  console.log(keys);
}

This should display a numeric key on the execution log instead of undefined.

Reference:

Generate Verification Codes


Solution 2:

You get that error because the items array is empty. Before grabbing a value that is assumed to be there, first check if the element exists.

// Mock AdminDirectory
const AdminDirectory = {
  VerificationCodes: {
    list: (userId) => {
      const codes = [{
        userId: 'user@example.com',
        verificationCode: 'code1',
      }, {
        userId: 'user@example.com',
        verificationCode: 'code2',
      }];
      return { items: codes.filter(c => c.userId === userId) };
    },
  }
};

const response = AdminDirectory.VerificationCodes.list('user1@example.com');

// Check if the `items` array has anything before accessing the value
const code = response.items.length > 0 ? response.items[0].verificationCode : null;

console.log(code); // null

Post a Comment for "Chicken And Egg Situation With Retrieve 8-digit Codes From New Created Accounts"