Angular 项目上的 Firebase 按键
Angular Firebase push key on item
我是 angular 和 Firebase 的新手,我有些不懂。
我想在我的数据库中使用唯一键推送 url 图片。
现在我有一个 url 数组,我将它发送到我的数据库:
而不是 0,1,2 我想推送一个唯一的 ID
我的推送数据代码:
export class SpotsService {
spotsRef: AngularFireList<any>;
spotRef: AngularFireObject<any>;
spotiD: string;
picsUrl: string[];
picurl: string;
constructor(private db: AngularFireDatabase) {
this.picsUrl = [];
}
addSpot(spot: Spot){
this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
photo: spot.photo,
categories: spot.categories
});
}
}
我的界面:
export interface Spot {
$key: string;
title: string;
synopsis: string;
userId: string;
markerLatLng: number[];
markerLatLngTwo: number[];
photo: string[];
categories: string[];
}
如果有人能帮助我 :) 非常感谢
由于 photo
在您的 JavaScript 代码中声明为本地数组 (photo: string[];
),Firebase 将按原样写入数据库。
如果你想用推送 ID 写入单独的照片,你必须将它们与其他数据分开写入,并为它们中的每一个调用 push()
:
let newRef = this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
categories: spot.categories
});
spot.photo.forEach((photo) => {
newRef.child("photo").push(photo);
})
上面是最简单的,但是会导致对数据库进行多次writer操作。要在单个写入操作中获得相同的结果,您可以执行以下操作:
let photos = {};
spot.photo.forEach((photo) => {
photos[this.spotsRef.push().key] = photo;
})
this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
photo: photos,
categories: spot.categories
});
Firebase 的 Firestore 和实时数据库不允许在数组字段中使用自定义键。为此,您需要在文档中创建一个图像子集,您可以在其中为每个图像定义一个自定义 ID。
示例:
galeries (collection)
|_gallery_1 (document with custom id)
-- title: Christmas (field)
-- date: 12/25/2019 (field)
-- photos: (collection)
|_photo_1 (document with custom id)
-- url: https://.../img.png (field)
|_photo_2...
Youtube 上有一个很棒的 Firestore NoSQL 建模教程,强烈推荐:
https://www.youtube.com/watch?v=jm66TSlVtcc
我是 angular 和 Firebase 的新手,我有些不懂。 我想在我的数据库中使用唯一键推送 url 图片。 现在我有一个 url 数组,我将它发送到我的数据库:
而不是 0,1,2 我想推送一个唯一的 ID
我的推送数据代码:
export class SpotsService {
spotsRef: AngularFireList<any>;
spotRef: AngularFireObject<any>;
spotiD: string;
picsUrl: string[];
picurl: string;
constructor(private db: AngularFireDatabase) {
this.picsUrl = [];
}
addSpot(spot: Spot){
this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
photo: spot.photo,
categories: spot.categories
});
}
}
我的界面:
export interface Spot {
$key: string;
title: string;
synopsis: string;
userId: string;
markerLatLng: number[];
markerLatLngTwo: number[];
photo: string[];
categories: string[];
}
如果有人能帮助我 :) 非常感谢
由于 photo
在您的 JavaScript 代码中声明为本地数组 (photo: string[];
),Firebase 将按原样写入数据库。
如果你想用推送 ID 写入单独的照片,你必须将它们与其他数据分开写入,并为它们中的每一个调用 push()
:
let newRef = this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
categories: spot.categories
});
spot.photo.forEach((photo) => {
newRef.child("photo").push(photo);
})
上面是最简单的,但是会导致对数据库进行多次writer操作。要在单个写入操作中获得相同的结果,您可以执行以下操作:
let photos = {};
spot.photo.forEach((photo) => {
photos[this.spotsRef.push().key] = photo;
})
this.spotsRef.push({
title: spot.title,
synopsis: spot.synopsis,
userId: spot.userId,
markerLatLng: spot.markerLatLng,
markerLatLngTwo: spot.markerLatLngTwo,
photo: photos,
categories: spot.categories
});
Firebase 的 Firestore 和实时数据库不允许在数组字段中使用自定义键。为此,您需要在文档中创建一个图像子集,您可以在其中为每个图像定义一个自定义 ID。
示例:
galeries (collection)
|_gallery_1 (document with custom id)
-- title: Christmas (field)
-- date: 12/25/2019 (field)
-- photos: (collection)
|_photo_1 (document with custom id)
-- url: https://.../img.png (field)
|_photo_2...
Youtube 上有一个很棒的 Firestore NoSQL 建模教程,强烈推荐: https://www.youtube.com/watch?v=jm66TSlVtcc