I only want this to fire if there is no value for user.app_metadata.paid_account
but it is firing and updating every time a user logs in even if they already have this value!
This previously worked fine.
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
if (!user.app_metadata.paid_account) {
user.app_metadata.paid_account = false;
var date = new Date();
user.app_metadata.trial_end_date = date.setDate(date.getDate() + 7);
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function(){
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
}
}
@dylan can you clarify the following:
- How have you verified that this is firing? Have you used the Realtime webtask logs extension, with console.log() outputs within the code block?
- Are you calling the callback anywhere outside of the
if
statement?
- Do you have any Hooks that may be populating the
user.app_metadata.paid_account
?
You’re applying a logical NOT operator to the expression user.app_metadata.paid_account
. The expression will be falsy when the property is undefined, but also for: null, false, -0, +0, NaN, “”.
Since you’re then setting the property to false
, next iterations will still trigger the condition because the falsy value is inverted to a truthy value by the logical NOT operator. If you only want to execute the body of the condition when the property is not yet defined you should instead use:
if (user.app_metadata.paid_account === undefined) {
// ....
}