このモジュールはAJAX機能で構成されていますが、私はクライアントコントローラにエクスポートしようとしていますが、モジュールが定義されていない、NodeJSとCloud9環境を使用しています。モジュールがNodeJSで定義されていません
AJAXモジュール
module.exports = {
appUrl: window.location.origin,
ready: function(fn) {
if(typeof fn !== 'function') {
return;
}
if(document.readyState === 'complete') {
return fn();
}
document.addEventListener('DOMContentLoaded', fn, false);
},
ajaxRequest: function(method, url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
callback(xmlhttp.response);
}
};
xmlhttp.open(method, url, true);
xmlhttp.send();
}
};
クライアントコントローラ
'use strict';
var ajaxFunctions = require("../common/ajax-functions.js");
(function(){
var profileId = document.querySelector('#profile-id') || null;
var profileUsername = document.querySelector('#profile-username') || null;
var profileRepos = document.querySelector('#profile-repos') || null;
var displayName = document.querySelector('#display-name');
var apiUrl = ajaxFunctions.appUrl + '/api/:id';
function updateHtmlElement(data, element, userProperty) {
element.innerHTML = data[userProperty];
}
ajaxFunctions.ready(ajaxFunctions.ajaxRequest('GET', apiUrl, function(data){
var userObject = JSON.parse(data);
updateHtmlElement(userObject, displayName, 'displayName');
if(profileId !== null) {
updateHtmlElement(userObject, profileId, 'id');
}
if(profileUsername !== null) {
updateHtmlElement(userObject, profileUsername, 'username');
}
if(profileRepos !== null) {
updateHtmlElement(userObject, profileRepos, 'publicRepos');
}
}));
})();
エラーメッセージにエラーが発生した行が表示されますか?ちょっと推測すると、 'var ajaxFunctions = require(" ../ common/ajax-functions.js ");が正しい場所を指していること、そして' ajax-functions.js'が有効なモジュールをエクスポートしていることを確認する必要があると思います。 – Schlaus
ajax-functions.jsの1行目にエラーが発生しました –
その行を投稿できますか? – Schlaus