【发布时间】:2021-08-20 17:08:37
【问题描述】:
我正在处理 API 获取请求。我创建了一个 POST 请求以在 firebase 实时数据库中添加数据。代码如下:
// CREATE POST
app.post("/post", (req, res) => {
let key;
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// User is signed in.
var newPost = firebase.database().ref("posts/");
var myPost = newPost.push({
createdBy: user.uid,
from: req.body.from,
to: req.body.to,
duration: req.body.duration,
comments: req.body.comments,
});
res.send(newPost);
const postId = myPost.key;
console.log(postId);
} else {
// No user is signed in.
res.status(404).send("No user is signed in right now!");
}
});
});
现在,为了得到一个具体的帖子,我写了如下代码:
// GET SPECIFIC POST
app.get("/post/:id", (req, res) => {
let response;
firebase
.database()
.ref("posts/" + req.params.id)
.on("value", (snapshot) => {
response = snapshot.val();
});
res.send(response);
});
我是 Firebase 的新手,所以我真的不知道如何获得特定的帖子。请帮帮我
【问题讨论】:
标签: node.js api firebase-realtime-database request crud