[英]Firebase Update specific fields?
Im updating my firebase database continuously with this code: 我用以下代码不断更新我的firebase数据库:
var admin = require("firebase-admin");
// Fetch the service account key JSON file contents
var serviceAccount = require("service.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "DATABASE_URL"
});
var db = admin.database();
var ref = db.ref("games");
var fs = require('fs');
var filePath = 'games/2017-06-27.json';
var file = fs.readFileSync(filePath);
fs.watchFile(filePath, function() {
var request = require('request');
var usersRef = ref.child('2017-06-27');
request('http://URL/games/2017-06-27.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var asJson = JSON.parse(body)
usersRef.update(asJson)
}
})
});
So I get a json with real-time sports data scores, game status etc (using another service). 所以我得到了一个带有实时体育数据比分,比赛状态等(使用其他服务)的json。 I then use node to check changes on the file where the data is stored and write to the firebase database. 然后,我使用node检查存储数据的文件上的更改并将其写入Firebase数据库。 It works fine, but I don't want to replace the DB. 它工作正常,但我不想替换数据库。 I just want to update the fields that change, because I have other service adding extra data to this child in the database that i want to keep. 我只想更新更改的字段,因为我还有其他服务将额外的数据添加到我要保留的数据库中的此子项中。 How can I update only the fields with changes without replacing the all Database and that way keep other extra data that don't come from this request? 如何在不更改所有数据库的情况下仅更新具有更改的字段,并以这种方式保留其他不是来自此请求的额外数据?
When you call update()
on a location, Firebase loops over the data that you pass in (in your case asJson
) and for each key performs a ref.child(key).set(value)
. 当您在某个位置调用update()
时,Firebase会循环传递传入的数据(在本例中为asJson
),并且对于每个键都执行ref.child(key).set(value)
。
Object.keys(asJson).forEach((key) => {
usersRef.child(key).set(asJson[key]);
})
So while it merges the top-level properties of asJson
with the existing data under usersRef
, it still replaces the data under each key. 因此,尽管它将asJson
的顶级属性与asJson
下的现有数据usersRef
,但仍替换了每个键下的数据。
If you want to merge on that level too, you'll have to convert the JSON from a tree into a list of paths. 如果您也想在该级别上进行合并,则必须将JSON从树转换为路径列表。 Say your JSON looks like this: 假设您的JSON如下所示:
{
user1: {
name: "Speed_John"
},
user2: {
name: "Frank van Puffelen"
}
}
You will have to convert this to: 您将不得不将其转换为:
{
"user1/name": "Speed_John",
"user2/name": "Frank van Puffelen"
}
var JSON = { user1: { name: "Speed_John" }, user2: { name: "Frank van Puffelen", id: 209103 } }; var updates = {}; function flatten(json, prefix) { Object.keys(json).forEach((key) => { var path = (prefix||"")+"/"+key; if (typeof json[key] === "object") { flatten(json[key], path); } else { updates[path] = json[key]; } }); } flatten(JSON); console.log(updates);
With this code you can then call: 使用此代码,您可以调用:
usersRef.update(updates);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.