Reputation: 72
I'm trying to use emailjs to send email to user after submission of form but I'm getting this error:
FAILED... ReferenceError: XMLHttpRequest is not defined
at C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\emailjs-com\cjs\api\sendPost.js:8:21
at new Promise (<anonymous>)
at Object.sendPost (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\emailjs-com\cjs\api\sendPost.js:7:12)
at Object.send (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\emailjs-com\cjs\methods\send\send.js:25:23)
at C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\routes\index.js:197:11
at Layer.handle [as handle_request] (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\express\lib\router\layer.js:95:\lib\router\layer.js:95:5)
at C:\Users\Awesome\Desktop\Awesome\Websites\Client Websites\alumates-landing-page\node_modules\express\lib\router\index.js:281:22
According to the error, the XMLHttpRequest is not defined in emailjs's sendpost.js
I did not modify this file. I just installed emailjs the regular way using
npm install emailjs-com --save
Please, anybody know how to fix? This is my route:
router.post('/groups/join', function (req, res) {
//handle form submission here
var templateParams = {
url: url,
group_name: group_name, //gotten from form submission value
user_name: joinRequest.user_name, //gotten from form submission value
email: joinRequest.email //gotten from form submission value
}
emailjs.send(serviceID, groupID, templateParams)
.then(function (response) {
console.log('SUCCESS!', response.status, response.text);
res.redirect("/login")
}, function (error) {
console.log('FAILED...', error);
});
})
I have also required emailjs and xhr at the top of my route:
var xhr = require("xhr");
var emailjs = require("emailjs-com");
emailjs.init("user_CPxhncQgw2s5e4nNwbu36")
Upvotes: 1
Views: 3263
Reputation: 51
Gotcha I have a solution,
Tight Your sit, You are going to modify one file into the node_modules folder.
first you need to install xhr2
npm install xhr2
var XMLHttpRequest = require('xhr2');
const xhr = new XMLHttpRequest();
That's it.
Upvotes: -1
Reputation: 22993
None of this makes any sense. Email.js targets browsers and their APIs. Importing xhr at the top makes no difference; it does not make XHR APIs available in the global scope of Node.
If you REALLY wanted to use it you would need to stub the XHR implementation by using the xhr
package to create the skeleton, but it is really the wrong way to go. Just find an email lib targeting Node to begin with.
Upvotes: 1
Reputation: 376
You have to move the emailjs.send
method to front-end. I even installed xhr2 and replaced the code in the module. I got a log saying emailjs
does not work on non-browser applications.
Upvotes: 3
Reputation:
For using XHR in node js you need to use an external library like xhr2 First, install it
npm install xhr2
Now, require it
var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
Upvotes: 0