简体   繁体   中英

ReferenceError: XMLHttpRequest is not defined in emailjs

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")

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();

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.

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.

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
  1. go to node_modules/emailjs-com/cjs/api
  2. open sendPost.js file
  3. it may look like something this

在此处输入图像描述

  1. Now add this two line at some appropriate space, on the top of sendPost.js file
 var XMLHttpRequest = require('xhr2');
 const xhr = new XMLHttpRequest();

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM