[英]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.