我如何使用其密钥 firebase 检索数据
How do i retrieve data with its key firebase
假设我们有一个数据库:
Recipes
recipe_key(some uniq key generated when data pushed)
ingredients:pepper
如何使用 recipe_key 检索胡椒值?我检查了 angularfire2 的文档,它说使用 snapshotchanges 但是使用这段代码我只能得到“食谱”。我想我需要再往下一层?
constructor(afDb: AngularFireDatabase) {
afDb.object('/recipes/').snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
}).subscribe(item => console.log(item.$key));
}
文档是正确的,您应该使用快照更改,以便您可以从有效负载对象中获取密钥 属性。
查看您的代码,我相信您忘记将密钥添加到 AngularFireDatabase
的 object()
方法中
const recipeKey = '<your-push-key>';
afDb.object(`/recipes/${recipeKey}`).snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
}).subscribe(item => console.log(item.$key));
如果你想用 $key 属性 检索一个列表,你可以使用这个方法。
afDb.list(`/recipes`).snapshotChanges().map(actions => {
return actions.map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
});
}).subscribe(items => console.log(items));
假设我们有一个数据库:
Recipes
recipe_key(some uniq key generated when data pushed)
ingredients:pepper
如何使用 recipe_key 检索胡椒值?我检查了 angularfire2 的文档,它说使用 snapshotchanges 但是使用这段代码我只能得到“食谱”。我想我需要再往下一层?
constructor(afDb: AngularFireDatabase) {
afDb.object('/recipes/').snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
}).subscribe(item => console.log(item.$key));
}
文档是正确的,您应该使用快照更改,以便您可以从有效负载对象中获取密钥 属性。
查看您的代码,我相信您忘记将密钥添加到 AngularFireDatabase
object()
方法中
const recipeKey = '<your-push-key>';
afDb.object(`/recipes/${recipeKey}`).snapshotChanges().map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
}).subscribe(item => console.log(item.$key));
如果你想用 $key 属性 检索一个列表,你可以使用这个方法。
afDb.list(`/recipes`).snapshotChanges().map(actions => {
return actions.map(action => {
const $key = action.payload.key;
const data = { $key, ...action.payload.val() };
return data;
});
}).subscribe(items => console.log(items));