2015-12-13 86 views
0

我可以在JavaScript 檢索數據:players.child(key).exists(); 不過的NodeJS返回數據的這種交戰檢查是否有無效鍵的JavaScript火力地堡

FIREBASE WARNING: Exception was thrown by user callback. Error: Firebase.child failed: First 
argument was an invalid path: "". Paths must be non-empty strings and can't contain ".", "#", 
"$", "[", or "]"                    
    at Error (<anonymous>)                 
    at gg (/var/node/node_modules/firebase/lib/firebase-node.js:136:59)      
    at T.u (/var/node/node_modules/firebase/lib/firebase-node.js:246:216)      
    at /var/node/server.js:24:19                
    at Object.manager.shots.objIntegrity (/var/node/server.js:26:7)       
    at /var/node/server.js:39:33                
    at /var/node/node_modules/firebase/lib/firebase-node.js:46:642       
    at Bb (/var/node/node_modules/firebase/lib/firebase-node.js:43:165)      
    at xb (/var/node/node_modules/firebase/lib/firebase-node.js:22:216)      
    at yb (/var/node/node_modules/firebase/lib/firebase-node.js:21:1260)      

/var/node/node_modules/firebase/lib/firebase-node.js:43          
ion(){N("Exception was thrown by user callback.",b.stack||"");throw b;},Math.f    
                    ^      
Error: Firebase.child failed: First argument was an invalid path: "". Paths must be non-empty 
strings and can't contain ".", "#", "$", "[", or "]"           
    at Error (<anonymous>)                 
    at gg (/var/node/node_modules/firebase/lib/firebase-node.js:136:59)      
    at T.u (/var/node/node_modules/firebase/lib/firebase-node.js:246:216)      
    at /var/node/server.js:24:19                
    at Object.manager.shots.objIntegrity (/var/node/server.js:26:7)       
    at /var/node/server.js:39:33                
    at /var/node/node_modules/firebase/lib/firebase-node.js:46:642       
    at Bb (/var/node/node_modules/firebase/lib/firebase-node.js:43:165)      
    at xb (/var/node/node_modules/firebase/lib/firebase-node.js:22:216)      
    at yb (/var/node/node_modules/firebase/lib/firebase-node.js:21:1260) 

我的結構是

{ 
    players{ 
     -K5MOHoOKvxL8_tcRfmc{ 
     et... 
     } 
    } 
} 

感謝你的幫助

Vinc

回答

4

使用.child()時,不能指定密鑰"", ".", "#", "$", "[", or "]"

錯誤指出您嘗試傳入空字符串。要解決此錯誤,您需要檢查無效密鑰。

function isValidKey(key) { 
    var invalidKeys = { '': '', '$': '$', '.': '.', '#': '#', '[': '[', ']': ']' }; 
    return invalidKeys[key] === undefined; 
} 

var key = ""; 
var playersRef = new Firebase('<my-firebase-db>/players'); 
if (isValidKey(key)) { 
    playersRef.child(key).once('value', function(snapshot) { 
    console.log(snapshot.exists()); 
    }); 
} 

一兩件事從原始片段要注意,在.exists()功能是DataSnapshots,不References。要檢查數據是否存在,您需要撥打.once()電話並檢查返回的DataSnapshot