简体   繁体   English

ReferenceError:XMLHttpRequest 未在 emailjs 中定义

[英]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:我正在尝试使用 emailjs 在提交表单后将 email 发送给用户,但出现此错误:

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根据报错,emailjs的sendpost.js中没有定义XMLHttpRequest

I did not modify this file.我没有修改这个文件。 I just installed emailjs the regular way using我刚刚使用常规方式安装了 emailjs

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:我还需要在我的路线顶部使用 emailjs 和 xhr:

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要在 node js 中使用 XHR,您需要使用像 xhr2 这样的外部库 首先,安装它

 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.您必须将emailjs.send方法移动到前端。 I even installed xhr2 and replaced the code in the module.我什至安装了 xhr2 并替换了模块中的代码。 I got a log saying emailjs does not work on non-browser applications.我收到一条日志,说emailjs在非浏览器应用程序上不起作用。

None of this makes any sense.这些都没有任何意义。 Email.js targets browsers and their APIs. Email.js 针对浏览器及其 API。 Importing xhr at the top makes no difference;在顶部导入 xhr 没有区别; it does not make XHR APIs available in the global scope of Node.它不会使 XHR API 在 Node.js 的全局范围内可用。

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.如果您真的想使用它,则需要通过使用xhr包来创建骨架来存根 XHR 实现,但这确实是错误的方法。 Just find an email lib targeting Node to begin with.只需找到一个针对 Node 的电子邮件库即可。

Gotcha I have a solution,明白了,我有一个解决方案,

Tight Your sit, You are going to modify one file into the node_modules folder.坐好,你将修改一个文件到 node_modules 文件夹中。

first you need to install xhr2首先你需要安装xhr2

 npm install xhr2
  1. go to node_modules/emailjs-com/cjs/api go 到 node_modules/emailjs-com/cjs/api
  2. open sendPost.js file打开 sendPost.js 文件
  3. it may look like something this它可能看起来像这样

在此处输入图像描述

  1. Now add this two line at some appropriate space, on the top of sendPost.js file现在在 sendPost.js 文件顶部的某个适当位置添加这两行
 var XMLHttpRequest = require('xhr2');
 const xhr = new XMLHttpRequest();

That's it.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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