[英]Avoid triggering Firebase functions by real-time database on special cases
[英]auto increment Id generator in firebase real-time database
我正在使用以 express.js 作為后端的 firebase 實時數據庫。 這是我的數據保存代碼段:
const db = require('./firebase_connection').firebaseDatabase
async function saveData(req, model) {
await db.ref(model).set(req)
return {"statusCode": "201", "message": "Inserted successfully"}
}
module.exports = { saveData }
這是 json output:
{
"user" : {
"email" : "TEST77@TEST.com",
"full_name" : "Hossein Heydari",
"password" : "test",
"phone_number" : 54,
"role" : "client"
}
}
但是我還需要一個 ID 自動添加到數據庫中,就像MongoDB
的_id
一樣
我需要 firebase 來存儲任何 model 的自動增量 ID 的數據,我該怎么辦?
因此,firebase 確實提供了一種在您推送更改時獲取唯一密鑰的方法,我不確定您是否嘗試過:
postsRef.push({
author: "gracehop",
title: "Announcing COBOL, a New Programming Language"
});
// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push();
// Get the unique key generated by push()
var postId = newPostRef.key;
您可以在此處查看更多詳細信息。
或者,如果您想創建與 Mongo 所做的類似的操作,那么您應該創建一個自定義密鑰,創建一個與 Mongo 類似的密鑰並將其保存在數據庫中,並在每次新插入時將該 ID 添加到 model。 你可以在這里查看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.