Javascript 未填充自动完成列表

Javascript 未填充自动完成列表,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,根据下面的代码,我正在尝试用列表填充我的自动完成文本框。当我console.log()列表时,我得到了预期的结果,但是当我将列表添加到我的自动完成函数时,它没有按预期工作,我只看到列表名称作为建议。截图: 守则: var childData; // Holds Objects var exercises = []; // Push exercise names into this // Obtain Exercises function exerciseQueries() { var

根据下面的代码,我正在尝试用列表填充我的自动完成文本框。当我
console.log()
列表时,我得到了预期的结果,但是当我将列表添加到我的自动完成函数时,它没有按预期工作,我只看到列表名称作为建议。截图:

守则:

var childData; // Holds Objects
var exercises = []; // Push exercise names into this
// Obtain Exercises
function exerciseQueries() { 
    var ref = firebase.database().ref('exercises'); // Reference equal to exercises node
    ref.orderByChild('name').on('value', function (snapshot) { // Order by name of exercise
        snapshot.forEach(function (childSnapshot) { // Loop, on each hit
            // childData will be the actual contents of the child
            childData = childSnapshot.val();
            console.log(childData);    
            exercises.push(childData.name);
        });

    });
}
exerciseQueries(); // Execute function


$(function () {
    $('input.autocomplete').autocomplete({
        data: {childData: null}
    });
    console.log(exercises);    
});
我试过传递一个对象(childData)和列表(练习);但两者都不起作用。我还尝试了源参数而不是数据,但没有成功。有人能帮忙吗

编辑更新的代码(相同错误)


上的
是异步的。将
$(函数(){…}
移动到
快照之后。forEach
@cartant我仍然收到相同的错误,我更新了我的原始帖子以显示新代码;我是否犯了错误?你不应该对
练习
数组做些什么吗?如果你在
$(函数(){…}之前记录它
你应该看到它不是空的——只要数据库中有东西——那么你只需要对它做点什么。@cartant Yessir我试过了。console.log(exercises)显示了我数据库中的所有三个练习。但是当我在“自动完成”字段中键入这些名称时,它们不会显示出来。它只显示“exercises”,但不是它的内容;这是我迷路的地方。对不起,我不熟悉jQuery和
自动完成功能。不过,至少这已经解决了问题的第一部分。
function exerciseQueries() {
    var ref = firebase.database().ref('exercises'); // Reference equal to exercises node
    ref.orderByChild('name').on('value', function (snapshot) { // Order by name of exercise
        snapshot.forEach(function (childSnapshot) { // Loop, on each hit
            // childData will be the actual contents of the child
            childData = childSnapshot.val();
            console.log(childData);
            exercises.push(childData.name);
        });
        $(function () {
            $('input.autocomplete').autocomplete({
                data: {childData}
            });
        });

    });
}