使用set()时,Firebase不会添加完整的对象

时间:2016-07-22 22:42:30

标签: javascript arrays firebase firebase-realtime-database

我正在开发Chrome扩展程序,并将经过Google身份验证的用户的数据推送到Firebase。

我正在侦听来自另一个JS文件的消息,当它进来时,我想要获取“用户配置文件”对象(请求)并将属性添加到它,称为“visitedLinks”。 visitedLinks应设置为空对象。

我在整个代码中执行了3个console.logs,在所有三种情况下,console.logs都将“visitedLinks”设置为空对象,但是当我推送到Firebase时,“visitedLinks”不是属性。 / p>

//Relevant 3 console statements are the following
//console.log('request.accountData = ', request.accountData)
//console.log('userObject test #1 = ', userObject)
//console.log('userObject = ', userObject)

var rootRef = new Firebase("https://search-feed-35574.firebaseio.com/");

if (localStorage.userIsAuthenticated) {
    console.log('user is authenticaled')
    chrome.runtime.onMessage.addListener(
        //listen for messages
        function(request, sender, sendResponse) {
            //url is coming in from a content script, use localStorage.uid to make database call
            if (request.url) {
                console.log('message coming from content script')
                var uid = localStorage.uid;
                var url = request.url;
                var userRef = rootRef.child(uid);
                newLinkRef = userRef.push(url);
                //otherwise, we're getting a message from popup.js, meaning they clicked it again, or they've signed in for the first time
            } else {
                console.log('message coming from popup')
                //here we're passing in all the data from the person's Google user account
                var googleUID = request.accountData.uid
                    //then we make a new object with the key as their google UID and the value all the account data
                request.accountData.visitedLinks = {}
                console.log('request.accountData = ', request.accountData)
                var userObject = {};
                userObject[googleUID] = request.accountData;
                console.log('userObject test #1 = ', userObject)
                //here were checking to see if the UID is already a key in the database
                //basically, if they've ever logged in
                rootRef.once('value', function(snapshot) {
                    if (snapshot.hasChild(googleUID)) {
                        //user has authenticated before, they just happened to click the popup again
                        console.log('already authenticated, you just clicked the popup again')
                    } else {
                        console.log('users first DB entry');
                        //if they're not in the database yet, we need to push userObject to the DB
                        //and push their current url to the publicLinks array
                        rootRef.set(userObject, function(error) {
                            console.log('error = ', error);
                            console.log('userObject after DB insert = ', userObject)
                        });
                    }
                })
            }

            //figure out if this user has entries in the DB already
            //just push the link information onto the "links" node of the db object
            //if not, push a ref (to the right place)
            // console.log(sender)
        });
} else {
    console.log('user isnt authenticated')
}

1 个答案:

答案 0 :(得分:1)

事实证明,您无法将空对象插入数据库。类似的问题在这里回答:Create an empty child record in Firebase