Firebase无限循环在child_added / set上插入我的项目

时间:2016-09-28 07:18:08

标签: firebase firebase-realtime-database vue.js

我有一个无限循环将项目插入我的Firebase,所以当我点击我的帖子表单时,它会插入我的项目,直到我终止该过程。你能帮我解决一下吗?

PS:我正在使用VueJS

            var usersRef = new Firebase('https://xxxxxxxxxxxxxxxxxx.firebaseio.com/userslist/');

            var vm = new Vue({
                el: '#list1',
                data: function () {
                    return{
                        // Initialisation du tableau de users
                        users: [],
                        sortKey: 'id',
                        reverse: 1,
                        nextKey: null
                    };

                },
                ready: function () {
                    // this works
                    //this.sortKey = 'name';
                },
                methods: {
                    updateUsers: function () {
                    },
                    removeUser: function (item) {
                        usersRef.child(item.id).remove();
                    },
                    addItem: function (e) {
                        e.preventDefault();
                        // get form data as Json
                        var jsonData = ConvertFormToJSON('form_add');
                        //console.log(jsonData);//test ok

                        //get the last item id and save it to next key

                        usersRef.limitToLast(1).on('child_added', function (snapshot) {
                            var lastKey = parseInt(snapshot.key());
                            this.nextKey = lastKey + 1;
                            console.log('nextKey ' + nextKey);//test ok
                            //
                            // save data to firebase
                            usersRef.child(this.nextKey).set(jsonData, function (snap) {
                             //console.log('add success');//test
                             //Notification par Jquery
                             var itemAdded = snap.val();
                             $.notify(itemAdded.firstname + " " + itemAdded.name + " à été ajouté", "success", {position: "top right"});
                             this.pendingKey = 0;
                             });
                        });

                    },
                    // Tri des colonnes
                    sortBy: function (_sortKey) {
                        this.reverse = (this.reverse == -1) ? 1 : -1;
                        this.sortKey = _sortKey;
                        console.log("SortKey " + this.sortKey);
                    }
                }
            });


            usersRef.on('child_added', function (snapshot) {

                var item = snapshot.val();
                item.id = snapshot.key();
                console.log('id ' + item.id);

                vm.users.push(item);

            }, function (errorObject) {
                console.log("The read failed: " + errorObject.code);
            });

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案:

            //get the last item id and save it to next key
            var _nextKey = this.nextKey;
            usersRef.limitToLast(1).on('child_added', function (snapshot) {
                var lastKey = parseInt(snapshot.key());
                _nextKey = lastKey + 1;

            });
            this.nextKey = _nextKey;
            console.log('nextKey ' + this.nextKey);//test ok
            // save data to firebase
            usersRef.child(this.nextKey).set(jsonData, function (snap) {
                //console.log('add success');//test
                //Notification par Jquery
                var itemAdded = snap.val();
                $.notify(itemAdded.firstname + " " + itemAdded.name + " à été ajouté", "success", {position: "top right"});
            });

可以帮助别人!