【问题标题】:return array from function javascript从函数javascript返回数组
【发布时间】:2014-11-20 02:42:28
【问题描述】:

我需要返回我的行为,但它不起作用

function getBehaviorList() {
    var behaviors = getBehaviors();
    console.log("Making behavior list");
    console.log(behaviors);
    $.each(behaviors, function (index, value) {
        if (value.indexOf("User/.lastUploadedChoregrapheBehavior") < 0) {
            $('#installedBehaviors tr:last').after('<tr><td>' + value.split('/').pop() + '</td><td><a href="#" class="btn btn-default" id="' + value + '"><span class="text-color-green glyphicon glyphicon-play-circle"></span> Start</a></td></tr>');
        }
    });
    $("#installedBehaviors a").click(function () {
        startBehavior(this);
    });
}
function getBehaviors() {
    console.log("Getting behaviors");
    session.service("ALBehaviorManager").done(function (behaviorManager) {
        behaviorManager.getUserBehaviorNames().done(function (behaviors) {
            behaviors;
            console.log(behaviors);
            return behaviors;
        });
    }).fail(function (error) {
        console.log("An error occurred: ", error);
    });
}

这是我在控制台中遇到的错误

获取行为

制作行为列表

未定义

未捕获的类型错误:无法读取未定义的属性“长度”

["User/.lastUploadedChoregrapheBehavior/test","User/actura-test/test", "User/check-update", "User/follow-me"]

有人知道为什么吗?

【问题讨论】:

  • 您不能从函数内部的回调中间返回值。这很可能是一个异步调用(实际上是 2 个嵌套的异步调用)。
  • 有什么方法可以制作出来吗?

标签: javascript jquery arrays return


【解决方案1】:

您不能从函数内部的回调中间返回值。这很可能是一个异步调用(或两个它出现)。

您可以使用延迟或承诺来做到这一点。下面是一个使用回调的简单示例:

function getBehaviorList() {
    getBehaviors(function (behaviors) {
        console.log("Making behavior list");
        console.log(behaviors);
        $.each(behaviors, function (index, value) {
            if (value.indexOf("User/.lastUploadedChoregrapheBehavior") < 0) {
                $('#installedBehaviors tr: last ').after(' < tr > < td > ' + value.split(' / ').pop() + ' < /td><td><a href="#" class="btn btn-default" id="' + value + '"><span class="text-color-green glyphicon glyphicon-play-circle"></span > Start < /a></td > < /tr>');
            }
        });
        $("#installedBehaviors a").click(function () {
            startBehavior(this);
        });
    });
}

function getBehaviors(callback) {
    console.log("Getting behaviors");
    session.service("ALBehaviorManager").done(function (behaviorManager) {
        behaviorManager.getUserBehaviorNames().done(function (behaviors) {
            console.log(behaviors);
            callback(behaviors);
        });
    }).fail(function (error) {
        console.log("An error occurred: ", error);
        callback();  // Possibly callback with [] instead.
    });
}

使用延迟可能是一种更好的模式,但需要做更多的工作,而且我需要更多地了解您正在调用的服务。

【讨论】:

  • 它是 Aldebaran 为 NAO 机器人提供的服务community.aldebaran-robotics.com/doc/2-1/dev/js/index.html 使用回调我得到一个错误 Uncaught ReferenceError: callback is not defined --- 我的错误只是忘记了回调.. 谢谢
  • 你把第一部分改成getBehaviors(function (behaviors) {等了吗?
  • 我没有注意到这部分函数 getBehaviors(callback) { 但修复它使它一切正常,非常感谢:)
  • 啊哈.. 这是让一切顺利的关键。它将你所有的代码变成一个回调,当另一个函数最终完成时被调用。很高兴它有帮助:)
【解决方案2】:

你被调用 getBehaviours() --> 获取行为

接下来您将进行异步调用(它没有及时的直接响应,但稍后会。

您返回 getBehaviorList 时没有任何返回值(未定义),它没有长度。

然后您会从 getUserBehaviorNames 和 console.log(behaviors) 收到答案。

你需要从 .done() 调用一个新函数

function continueBuildBehaviorList(behaviors) {
    $.each(behaviors, function (index, value) {
        if (value.indexOf("User/.lastUploadedChoregrapheBehavior") < 0) {
            $('#installedBehaviors tr:last').after('<tr><td>' + value.split('/').pop() + '</td><td><a href="#" class="btn btn-default" id="' + value + '"><span class="text-color-green glyphicon glyphicon-play-circle"></span> Start</a></td></tr>');
        }
    });
    $("#installedBehaviors a").click(function () {
        startBehavior(this);
    });
}

【讨论】:

  • 这个例子听起来很有趣,但并不完整。
  • 不。一些工作可以由用户完成。我解释了这个问题并指出了一条路要走。但这并不意味着完整。仍然有效:)
  • 仍然有效如果你写了缺失的部分你的意思是:)
猜你喜欢
  • 1970-01-01
  • 2014-08-29
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
相关资源
最近更新 更多