我是firebase的新手。 我想要获取子数据和密钥。 我在nodejs Environment中尝试了以下代码。
var firebase = require("firebase");
firebase.initializeApp({
databaseURL: "https://some.firebaseio.com/"
});
var customer = firebase.database().ref().child('customer');
const fname = "name";
const lname = "title";
customer.orderByChild("partnerFirstName").equalTo(fname).on('child_added', function (snapshot) {
console.log(snapshot.val());
var fn = snapshot.val().partnerFirstName;
var custdata = snapshot.val();
console.log(custdata);
console.log("FirstName:"+fn);
if (custdata.partnerFirstName === fn) {
console.log(custdata);
}
});
输出如下
{
createdAt: '2018-03-20T23:25:35.212Z',
partnerEmail: 'beard@yahoo.com',
partnerFirstName: 'name',
partnerGender: 'male',
partnerLastName: 'title',
partnerPhone: '444-602-4444',
profilePicture: 'assets/profile_icon.png',
uid: 'x99wl8kkmJSENpNdVMXTXCh7amy2' }
FirstName: name
在这种情况下,我们没有得到记录的键值,但是firstname没有任何错误。
但是当我们尝试以下代码时,密钥随数据一起出现,但是firstname将作为未定义
var customer = firebase.database().ref().child('customer');
const fname = "Name";
const lname = "Title";
customer.orderByChild("partnerFirstName").equalTo(fname).on('value', function (snapshot) {
console.log(snapshot.val());
var fn = snapshot.val().partnerFirstName;
var custdata = snapshot.val();
console.log(custdata);
console.log("FirstName:"+fn);
if (custdata.partnerFirstName === fn) {
console.log(custdata);
}
});
输出如下
{ '-L84iSPyr9bPvnJZ6BgV':
{
createdAt: '2018-03-20T23:25:35.212Z',
partnerEmail: 'beard@yahoo.com',
partnerFirstName: 'name',
partnerGender: 'male',
partnerLastName: 'title',
partnerPhone: '444-602-4444',
profilePicture: 'assets/profile_icon.png',
uid: 'x99wl8kkmJSENpNdVMXTXCh7amy2' } }
FirstName: undefined
如何获取子数据以及密钥?
答案 0 :(得分:2)
要获取密钥,请尝试以下操作:
var ref = firebase.database().ref("customer").orderByChild("partnerFirstName").equalTo(fname);
ref.once("value").then(function(snapshot) {
snapshot.forEach(function(child){
var key = child.key;
var partnerName=child.val().partnerFirstName;
});
});
在上面,您在customer
的子项内部进行迭代,然后使用var key = child.key;
答案 1 :(得分:1)
您应该用
替换您的代码customer.orderByChild("partnerFirstName").equalTo(fname).on('value',
function (snapshot) {
var snap = snapshot.val();
var key = Object.keys(snap);
var custdata = snap[key];
var fn = snap[key].partnerFirstName;
if (custdata.partnerFirstName === fn) {
console.log(custdata);
}
});
返回密钥和子项
答案 2 :(得分:0)
感谢大家。我成功解决了这段代码的问题。我的主要动机是使用上面的代码通过firebase云函数获取json格式。它工作正常。我成功搜索了url中的firstname和lastname,它取得了相应的记录。
exports.getCustomerbyname2 = functions.https.onRequest((req, res) => {
res.header('Content-Type', 'application/json');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.status(204).send('');
}
const fname = req.query.fname;
const lname = req.query.lname;
var customer=admin.database().ref().child('customer');
customer.on('child_added', function (snapshot) {
var fname1 = snapshot.val().partnerFirstName;
var lname1 = snapshot.val().partnerLastName;
customer.orderByChild('partnerFirstName').equalTo(fname).once('value', function(snapshot) {
var arr=[];
if (fname1 === fname && lname1===lname) {
arr.push(snapshot.val());
console.log(snapshot.val());
res.status(200).json({ customer: arr});
}else{
res.status(200).send('ERROR : ' + errorObject);
}
},function(errorObject){
console.log("The read failed: " + errorObject.code);
// res.status(200).send('ERROR : ' + errorObject);
});
});
});