如何在 Node.js 中获取由 firebase 实时数据库填充的下拉(组合框)值?
How to get drop down (combo box) value which is populated by firebase realtime database in Node.js?
我检查了几个关于堆栈溢出的答案,但 none 似乎回答了我的问题。
我像这样使用 firebase 填充了下拉列表(HTML Select 和选项):
节点
function getClients() {
const ref = firebaseApp.database().ref('clients');
return ref.once('value').then(snap => snap.val());
}
app.get('/clients', (request,response) => {
// response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
getClients().then(clients => {
response.render('clients', { clients })
});
})
HTML(带车把)
<label for="client">Client</label>
<select id="client" name="client">
<option>Choose Client</option>
{{#each clients}}
<option value="{{this}}">{{name}}</option>
{{/each}}
</select>
下拉列表已正确填充,但我的问题是要在 option
值中写入什么,以便能够获得子项 key
。
编辑(添加 json 相当于我的数据库结构)
{
"clients" : {
"-M0wyt-_2cC2nACDArUR" : {
"email" : "c",
"name" : "a",
"number" : "b"
},
"-M0wzLp-7p1qboJZ1srB" : {
"email" : "e",
"name" : "q",
"number" : "w"
}
}
}
在此先感谢您的帮助。
您可能需要更改渲染:
getClients().then(clients => {
const clientArray = Object.keys(clients)
.map(client => ({key: client, ...clients[client]}))
console.log(clientArray)
response.render('clients', { clients: ClientArray })
});
请参阅此代码段以了解此地图函数执行的数据转换。
const data = {
"clients" : {
"-M0wyt-_2cC2nACDArUR" : {
"email" : "c",
"name" : "a",
"number" : "b"
},
"-M0wzLp-7p1qboJZ1srB" : {
"email" : "e",
"name" : "q",
"number" : "w"
}
}
}
const clients = data.clients
const clientArray = Object.keys(clients)
.map(client => ({key: client, ...clients[client]}))
console.log(clientArray)
我检查了几个关于堆栈溢出的答案,但 none 似乎回答了我的问题。 我像这样使用 firebase 填充了下拉列表(HTML Select 和选项):
节点
function getClients() {
const ref = firebaseApp.database().ref('clients');
return ref.once('value').then(snap => snap.val());
}
app.get('/clients', (request,response) => {
// response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
getClients().then(clients => {
response.render('clients', { clients })
});
})
HTML(带车把)
<label for="client">Client</label>
<select id="client" name="client">
<option>Choose Client</option>
{{#each clients}}
<option value="{{this}}">{{name}}</option>
{{/each}}
</select>
下拉列表已正确填充,但我的问题是要在 option
值中写入什么,以便能够获得子项 key
。
编辑(添加 json 相当于我的数据库结构)
{
"clients" : {
"-M0wyt-_2cC2nACDArUR" : {
"email" : "c",
"name" : "a",
"number" : "b"
},
"-M0wzLp-7p1qboJZ1srB" : {
"email" : "e",
"name" : "q",
"number" : "w"
}
}
}
在此先感谢您的帮助。
您可能需要更改渲染:
getClients().then(clients => {
const clientArray = Object.keys(clients)
.map(client => ({key: client, ...clients[client]}))
console.log(clientArray)
response.render('clients', { clients: ClientArray })
});
请参阅此代码段以了解此地图函数执行的数据转换。
const data = {
"clients" : {
"-M0wyt-_2cC2nACDArUR" : {
"email" : "c",
"name" : "a",
"number" : "b"
},
"-M0wzLp-7p1qboJZ1srB" : {
"email" : "e",
"name" : "q",
"number" : "w"
}
}
}
const clients = data.clients
const clientArray = Object.keys(clients)
.map(client => ({key: client, ...clients[client]}))
console.log(clientArray)