【问题标题】:How to add a sub collection to a freshly created document in firebase?如何将子集合添加到firebase中新创建的文档?
【发布时间】:2020-03-06 13:14:43
【问题描述】:

我正在为 firebase 中的某些文档创建导入器。

我需要为我添加的每个文档的特定属性创建一个子集合(而不仅仅是一个数组)。

目前我有以下代码,但似乎不起作用:

let rawdata = fs.readFileSync("files/" + file);
let spot = JSON.parse(rawdata);
var spotFirebase = {
    id: spot.Id,
    sourceId: spot.SourceId,
    type: spot.Type,
    location: new firebase.firestore.GeoPoint(spot.Latitude, spot.Longitude),
    description: spot.Description,
    address: spot.Address,
    city: spot.City,
    country: spot.Country,
    price: spot.Price,
    parkingCost: spot.ParkingCost,
    opening: spot.Opening,
    name: spot.Name,
    features: spot.Features,
    activities: spot.Activities,
    rating: {
        ratingCount: spot.Rating.RatingCount,
        ratingAverage: spot.Rating.RatingAverage
    }
}

db.collection("spots").add(spotFirebase).then(function (docRef) {
    console.log("Document ", file, " written with ID: ", docRef.id, ", index: ", index, ". ", spot.Rating.UserRatings.length);

    spot.Rating.UserRatings.forEach(ur =>
        docRef.collection("userRatings").add(
            {
                date: ur.Date,
                username: ur.UserName,
                review: ur.Review,
                note: ur.Note,
                reviewSource: ur.ReviewSource
            }).then(function (subDocRef) {
                console.log("Review ID  ", subDocRef.id, " written");
            }).catch(function (errorReview) {
                console.error("Error adding document: ", errorReview);
            }));
})
.catch(function (error) {
    console.error("Error adding document: ", error);
});
  1. 我没有显示错误
  2. 我从未收到过“Review ID ... 已写”的消息
  3. 显然,我以写出的现场文档结束,但没有任何用户评分。

我想我没有正确添加子集合。

我做错了什么?

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    代码在终止之前没有等待子集合的添加完成。

    此编辑将添加到子集合中的内容纳入可测试函数,收集在 promises 数组中创建每个新文档的承诺,然后在终止之前等待所有这些 (Promise.all()) 完成...

    function addUserRating(parentRef, ur) {
      const newDoc = {
        date: ur.Date,
        username: ur.UserName,
        review: ur.Review,
        note: ur.Note,
        reviewSource: ur.ReviewSource
      }
      return parentRef.collection("userRatings").add(newDoc).then(result => {
        console.log("Review ID  ", result.path, " written");
        return result
      })
    }
    
    db.collection("spots").add(spotFirebase).then(function (docRef) {
      console.log("Document ", file, " written with ID: ", docRef.id, ", index: ", index, ". ", spot.Rating.UserRatings.length);
      let promises = spot.Rating.UserRatings.map(ur => addUserRating(docRef, ur))
      return Promise.all(promises)
    }).catch(function (error) {
      console.error("Error adding document(s): ", error)
    })
    

    【讨论】:

    • 我已经测试了您的解决方案,但仍然没有错误,也没有添加用户评分。
    • 你在日志中看到了什么?
    • 这让人怀疑spot.Rating.UserRatings 是一个空数组。我看到我的代码(和你的问题)中没有定义。您是否跳过了let spot = docRef.data() 的行?除非现场文档包含名为 Ratings 的对象并且该对象包含名为 UserRatings 的非空数组,否则我们不会看到循环运行。
    • 我也在想这个,这就是为什么我在日志中添加了spot.Rating.UserRatings.length,并且我的所有项目都有10到20个评分。也许要提供更多上下文:spot 是原始 JSON 文件,然后我基于 spot 构建 spotFirebase。我需要有不同的命名,另一种类型的一些属性(如GeoPoint),以及这个评级集合。我添加了这是如何填充的。
    • 这里你可以得到一个示例文件:drive.google.com/file/d/1uFrLPKgSa-fQ2deYVW7JGZbHxU-HnJ6E/…
    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2021-02-20
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多