简体   繁体   English

Uncaught TypeError:无法调用未定义的方法“ then”

[英]Uncaught TypeError: Cannot call method 'then' of undefined

I am trying to chain some events together so that each function will run after the previous one has finished. 我正在尝试将一些事件链接在一起,以便每个功能在上一个功能完成后运行。 This is how I have it: 这是我的方式:

function siteUsage() {
    // Do Something
}
function siteTerms() {
    // Do Something
}
function siteSources() {
    // Do Something
}

siteUsage().then(siteTerms()).then(siteSources());

But I get this error: 但是我得到这个错误:

Uncaught TypeError: Cannot call method 'then' of undefined 

Any ideas? 有任何想法吗? Am I also going about this the right way, I mean chaining Ajax requests like this? 我是否也在以正确的方式这样做,我的意思是像这样链接Ajax请求?

EDIT 编辑

Here is one of the functions in its entirety. 这是全部功能之一。 Incase you need to see what it does. 万一您需要查看它的功能。

function siteUsage() {

        $.getJSON('charts_ajax.php',{a : 'visits', rangeStartDate : '<?=$_POST["rangeStartDate"] ?>', rangeEndDate : '<?= $_POST["rangeEndDate"] ?>'}, function(data){
            if(data){
                var tableHtml = '<tbody><tr><td class="id" width="20%">Visits</td><td width="20%">' + data.visits + '</td><td width="60%">A visit is a single-user session.</td></tr>' +
                    '<tr><td class="id">New Visits</td><td width="20%">' + data.newVisits + '%</td><td>The percentage of visits marked as first-time visits.</td></tr></tbody>' +
                    '<tr><td class="id">Page Views</td><td>' + data.pageViews + '</td><td>Views of each individual page.</td></tr>' +
                    '<tr><td class="id">Average Pages per Visit</td><td>' +  data.avgPageViews + '</td><td>Page Views divided by Visits</td></tr>' +
                    '<tr><td class="id">Average Time On Site</td><td>' + data.avgTime + '</td><td>The average duration of visitor sessions.</td></tr>' +
                    '<tr><td class="id">Visitors</td><td>' + data.totalVisits + '</td><td>Total number of visitors to your website for the requested time period.</td></tr>' +
                    '<tr><td class="id">Visits from Mobile Devices</td><td colspan="2"><div style="position:relative; background:#9fb7cb; height:22px;"><span style="width:' + data.yesMobile + '%; background:#003F75; height:22px; display:inline-block; border-right:1px solid #fff;"></span><span style="font-size:85%; position:absolute; left:8px; top:4px; color:#fff; text-shadow:0 1px 0 #003F75; width:80px;"><strong>Yes:</strong> ' + data.yesMobile + '</span><span style="font-size:85%; position:absolute; right:8px; top:4px; color:#003F75;"><strong>No: </strong>' + data.noMobile + '</span></div></td></tr>';

                $('#usage-table').html(tableHtml).find('.spinner').stop();

            }
        });

    }

siteUsage has to return either a promise object or a deferred object. siteUsage必须返回承诺对象或延迟对象。 For example, 例如,

function siteUsage() {
    return $.getJSON(...);
}
function siteTerms() {
    return $.getJSON(...);
}
function siteSources() {
    return $.getJSON(...);
}

siteUsage().then(siteTerms).then(siteSources);

Additionally, as you can see in my code, .then accepts a function, therefore you'll want to pass the function rather than execute it (unless the function returns a function, which is unlikely for this use case) 此外,如您在我的代码中所见,.then接受一个函数,因此您将希望传递该函数而不是执行它(除非该函数返回一个函数,在这种情况下不太可能)

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

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