![](/img/trans.png)
[英]Why am I getting the error clearImmediate is not defined?
[英]Why Am I getting this “document is not defined” error?
我正在创建一个类似应用程序的表单,用户可以在其中输入文本并按下按钮,用户输入的测试将通过电子邮件发送给我。 我为此使用了 EmailJS,我的代码如下:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EmailJSResponseStatus_1 = require("./models/EmailJSResponseStatus");
exports.EmailJSResponseStatus = EmailJSResponseStatus_1.EmailJSResponseStatus;
var UI_1 = require("./services/ui/UI");
var _userID = null;
var _origin = 'https://api.emailjs.com';
function sendPost(url, data, headers) {
if (headers === void 0) { headers = {}; }
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (event) {
var responseStatus = new EmailJSResponseStatus_1.EmailJSResponseStatus(event.target);
if (responseStatus.status === 200 || responseStatus.text === 'OK') {
resolve(responseStatus);
}
else {
reject(responseStatus);
}
});
xhr.addEventListener('error', function (event) {
reject(new EmailJSResponseStatus_1.EmailJSResponseStatus(event.target));
});
xhr.open('POST', url, true);
for (var key in headers) {
xhr.setRequestHeader(key, headers[key]);
}
xhr.send(data);
});
}
function appendGoogleCaptcha(templatePrams) {
var element = document.getElementById('g-recaptcha-response');
if (element && element.value) {
templatePrams['g-recaptcha-response'] = element.value;
}
element = null;
return templatePrams;
}
/**
* Initiation
* @param {string} userID - set the EmailJS user ID
* @param {string} origin - set the EmailJS origin
*/
function init(userID, origin) {
_userID = userID;
_origin = origin || 'https://api.emailjs.com';
}
exports.init = init;
/**
* Send a template to the specific EmailJS service
* @param {string} serviceID - the EmailJS service ID
* @param {string} templateID - the EmailJS template ID
* @param {Object} templatePrams - the template params, what will be set to the EmailJS template
* @param {string} userID - the EmailJS user ID
* @returns {Promise<EmailJSResponseStatus>}
*/
function send(serviceID, templateID, templatePrams, userID) {
var params = {
lib_version: '2.4.1',
user_id: userID || _userID,
service_id: serviceID,
template_id: templateID,
template_params: appendGoogleCaptcha(templatePrams)
};
return sendPost(_origin + '/api/v1.0/email/send', JSON.stringify(params), {
'Content-type': 'application/json'
});
}
exports.send = send;
/**
* Send a form the specific EmailJS service
* @param {string} serviceID - the EmailJS service ID
* @param {string} templateID - the EmailJS template ID
* @param {string | HTMLFormElement} form - the form element or selector
* @param {string} userID - the EmailJS user ID
* @returns {Promise<EmailJSResponseStatus>}
*/
function sendForm(serviceID, templateID, form, userID) {
if (typeof form === 'string') {
form = document.querySelector(form);
}
if (!form || form.nodeName !== 'FORM') {
throw 'Expected the HTML form element or the style selector of form';
}
UI_1.UI.progressState(form);
var formData = new FormData(form);
formData.append('lib_version', '2.4.1');
formData.append('service_id', serviceID);
formData.append('template_id', templateID);
formData.append('user_id', userID || _userID);
return sendPost(_origin + '/api/v1.0/email/send-form', formData)
.then(function (response) {
UI_1.UI.successState(form);
return response;
}, function (error) {
UI_1.UI.errorState(form);
return Promise.reject(error);
});
}
exports.sendForm = sendForm;
exports.default = {
init: init,
send: send,
sendForm: sendForm
};
但是,当我运行它时,收到“文档未定义”错误“。另外,我正在调用 function emailjs.send() function send() 在上面的代码中定义,它说“文档未定义” . 他们所指的文档在 appendGoogleCaptcha() function 中,这与发送 email 有关。
我的理论是我需要定义 window 属性,以便它知道它试图找到的整个页面的哪个文档。 我做了 window.onload = function() {} 但这使得其中的所有函数都说“xyz 不存在”。 任何反馈将不胜感激
这里:
function appendGoogleCaptcha(templatePrams) {
var element = document.getElementById('g-recaptcha-response');
...
}
和这里:
function sendForm(serviceID, templateID, form, userID) {
if (typeof form === 'string') {
form = document.querySelector(form);
...
}
你不能这样做, document
在本机反应中不存在。 您可以通过引用替换它,但是如果此代码不是为 react-native 编写的,那么您将来可能会遇到更多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.