Reputation: 59
I am trying to get an 8-digit authentication code from the Google script API, and have gotten some help on another thread, and the following was recommended. Note: userEmail is a variable for the person's email address. =)
var codes = AdminDirectory.VerificationCodes.list(userEmail).items.forEach(function(item){Logger.log('Code: ' + item.verificationCode)});
it was mentioned as part of a function.
would it be possible to just call the API and retrieve an 8-digit code directly into a var?
like: var 8DigitCode = AdminDirectory.VerificationCodes.list(userEmail);
I have tried many different syntaxes, and either (a) I am really barking up the wrong tree, and it just won't work, Or I am using the wrong syntax =(
Upvotes: 0
Views: 108
Reputation: 9571
Your approach isn't working because .forEach()
doesn't return anything for the assignment.
The answer here really depends on what you're trying to do. If you just want the first returned code, then this will work by simply accessing the array element.
const code = AdminDirectory.VerificationCodes.list(id).items[0].verificationCode;
See in this example, there are multiple verification codes for the same user, but you'd be ignoring all but one.
// Mock AdminDirectory
const AdminDirectory = {
VerificationCodes: {
list: (userId) => {
const codes = [{
userId: '[email protected]',
verificationCode: 'code1',
}, {
userId: '[email protected]',
verificationCode: 'code2',
}];
return { items: codes.filter(c => c.userId === userId) };
},
}
};
// Get the first code
const code = AdminDirectory.VerificationCodes
.list('[email protected]')
.items[0]
.verificationCode;
console.log(code); // code1
I don't know the behavior of that API, but given that it can return multiple codes, you may want to be more selective and use something like Array.prototype.find()
.
Upvotes: 2