回调内部变量的异常行为?

时间:2019-02-04 12:23:32

标签: javascript callback

import Firebase from './Firebase'
import videoManager from './videoManage';

   function getAllDatabaseLocations() {
        ref = Firebase.database().ref("locations")
        var user_locations = [];
        ref.on("value", function (snapshot) {
            let location_obj = [];
            snapshot.forEach(function (datas) {
                const data = datas.val();

                // console.log("Video from FIREBASE URL IS : " + data.videourl);
                vid_manage = new videoManager(data.videourl);
                vid_ref = vid_manage.getLocationVideoUrl();
                vid_ref.getDownloadURL().then(function (url) {
                    videourl = url;
                   }).catch(function (error) {
                });
                let lokation = data.lokation;
                let videourl = data.videourl;
                let openinghours = data.openinghours;
                let links = data.links;

                let Lokationer = {
                    lokation: lokation,
                    videoUrl: videourl,
                    openingshours: openinghours,
                    links: links
                };

                // console.log("Location objects are: ", Lokationer);
                user_locations.push(Lokationer);
                // location_obj.push(Lokationer);

            });


        //    user_locations.push(location_obj);
        });
        console.log("User location:"+ JSON.stringify(user_locations));
        return user_locations;

    }

export default getAllDatabaseLocations;

我正在尝试将位置对象从Firebase收集到本地变量user_location。在回调中将user_locations内的Lokationer对象推入正常,但是返回回调后user_locations是空数组吗?因此,即使推送了Lokationer对象,返回值也始终为空数组。

user_locations在回调内部循环之后按预期方式打印,在回调外部不起作用吗?

0 个答案:

没有答案