这里有我从Firebase检索所有文档的代码,我只能检索文档中存在的数据,但我也想使用它检索文档ID,该怎么办?
Error Error: connect ECONNREFUSED 127.0.0.1:5432
这是我的吸气功能
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000;
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/users', async (req, res) => {
try {
const client = await pool.connect();
const result = await client.query('SELECT * FROM users');
const results = { 'results': (result) ? result.rows : null};
console.log(results);
res.status(200).json(results);
} catch (err) {
console.error(err);
res.send("Error " + err);
}
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
答案 0 :(得分:2)
要获取ID,请执行以下操作:
.map((DocumentSnapshot document) {
return new CustomCard(
docId : document.documentID,
name: document['name'],
phone: document['phno'],
service: document['Category'],
address: document['address'],
pin: document['pin'],
payment: document['Payment'],
);
}).toList(),
documentID
将为您提供每个文档ID。
答案 1 :(得分:0)
你也可以做类似的事情
snapshot.data!.docs[index].id
这将返回该索引处文档的 id