API Error in post registration hook

I have a post-registration hook enabled to call an api I have set up to send email. When I run it in the runner I get the following error message. When I run it normally and watch the logs, nothing errors, but the email doesn’t send.

Error:
{ “message”: “Unable to compile the extensibility code as javascript”, “statusCode”: 500, “error_description”: “Invalid or unexpected token”, “stack”: “/data/io/758c3e0e-2f8c-4b39-a2ff-f1cd92552dbc/webtask.js:4\nmethod: ‘POST’,\n ^\n\nSyntaxError: Invalid or unexpected token\n at createScript (vm.js:80:10)\n at Object.runInThisContext (vm.js:139:10)\n at WebtaskModule.compileWebtask (/data/sandbox/lib/module.js:140:30)\n at Object.defaultJavascriptCompiler [as nodejsCompiler] (/data/sandbox/lib/compiler.js:135:24)\n at extensibilityPointCompiler (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/compilerFactory.js:24:17)\n at /data/sandbox/lib/compiler.js:276:11\n at /data/sandbox/node_modules/async/dist/async.js:3880:24\n at replenish (/data/sandbox/node_modules/async/dist/async.js:1011:17)\n at /data/sandbox/node_modules/async/dist/async.js:1016:9\n at eachOfLimit (/data/sandbox/node_modules/async/dist/async.js:1041:24)” }

Code inside of hook:
module.exports = function(user, context, cb) {
var request = require(‘request’);
request({
method: ‘POST’,
url: ‘…/api/email’,
headers: {
‘Content-Type’: ‘application/json’,
},
body: { “emailAddress”: “${user.emailAddress}”,
“emailType”: “${user.user_metadata.nesletter}”,
“source”: “{test}”}
}, function(error, response, body) {
console.log(user)
console.log(context)
cb(error, response)
});
};

In my experience you’ll definitely want to see success in the test runner…

Longshot, but you may try a more explicit require statement.

var request = require('request@2.67.0')

Source https://auth0-extensions.github.io/canirequire/#request

Also noticed user.user_metadata.nesletter maybe should be user.user_metadata.newsletter ?

Let us know @molly.kelsey if suggestions from @jemery made the difference!

Changed the request version and fixed the typo. Still no success. It’s not even logging to the console.

Got it to work. There was an issue with one of the variables I was sending. user.emailAddress should have been user.email.

1 Like

Perfect! Glad to hear that!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.