Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将子集合添加到firebase中新创建的文档中?_Javascript_Node.js_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 如何将子集合添加到firebase中新创建的文档中?

Javascript 如何将子集合添加到firebase中新创建的文档中?,javascript,node.js,firebase,google-cloud-firestore,Javascript,Node.js,Firebase,Google Cloud Firestore,我正在为firebase中的某些文档创建导入程序 我需要为我添加的每个文档的特定属性创建一个子集合(而不仅仅是一个数组) 目前我有以下代码,似乎不起作用: 让rawdata=fs.readFileSync(“files/”+file); 让spot=JSON.parse(rawdata); var spotFirebase={ id:spot.id, sourceId:spot.sourceId, 类型:spot.type, 位置:新firebase.firestore.GeoPoint(点.纬

我正在为firebase中的某些文档创建导入程序

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

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

让rawdata=fs.readFileSync(“files/”+file);
让spot=JSON.parse(rawdata);
var spotFirebase={
id:spot.id,
sourceId:spot.sourceId,
类型:spot.type,
位置:新firebase.firestore.GeoPoint(点.纬度,点.经度),
描述:spot.description,
地址:spot地址:,
城市:地点,城市,
国家:地点,国家,
价格:现货,价格,
停车成本:spot.parkingCost,
开场白:现场,开场白,
姓名:spot.name,
特征:斑点特征,
活动:现场活动,
评级:{
评级计数:spot.Rating.ratingCount,
评级平均值:spot.Rating.ratingAverage
}
}
db.collection(“spots”).add(spotFirebase)。然后(函数(docRef){
log(“文档”,文件,“用ID:,docRef.ID,”,索引:,index,“.”,spot.Rating.UserRatings.length编写);
spot.Rating.UserRatings.forEach(ur=>
docRef.集合(“用户评级”)。添加(
{
日期:你的日期,
用户名:ur.username,
回顾:你的回顾,
注:你的注,
reviewSource:ur.reviewSource
}).then(函数(subDocRef){
console.log(“审查ID”,subDocRef.ID,“书面”);
}).catch(功能(错误审查){
console.error(“添加文档时出错:”,errorReview);
}));
})
.catch(函数(错误){
console.error(“添加文档时出错:”,错误);
});
  • 我没有显示错误
  • 我从来没有收到过“评论ID…书面”的信息
  • 显然,我以编写spot文档结束,但没有任何用户评级
  • 我想我没有正确添加子集合


    我做错了什么?

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

    此编辑将添加到子集合中的内容分解为可测试函数,收集在
    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)
    })
    

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

    此编辑将添加到子集合中的内容分解为可测试函数,收集在
    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()
    ?除非spot文档包含一个名为Ratings的对象,并且该对象包含一个名为UserRatings的非空数组,否则我们不会看到循环运行。我也在考虑这个问题,这就是为什么我在日志中添加了
    spot.Rating.UserRatings.length
    ,并且我的所有项都有10到20个评级。也许可以提供更多的上下文:
    spot
    是一个原始的JSON文件,然后我基于
    spot
    构建
    spotFirebase
    。我需要有不同的命名,一些其他类型的属性(如
    GeoPoint
    ),以及这个评级集合。我添加了填充方式。在这里您可以得到一个示例文件:我已经测试了您的解决方案,但仍然没有错误,也没有添加用户评级。您在日志中看到了什么?因此有人怀疑
    spot.Rating.UserRatings
    是一个空数组。我看到我的代码(以及你的问题)中没有定义。您是否跳过了一行,上面写着
    let spot=docRef.data()
    ?除非spot文档包含一个名为Ratings的对象,并且该对象包含一个名为UserRatings的非空数组,否则我们不会看到循环运行。我也在考虑这个问题,这就是为什么我在日志中添加了
    spot.Rating.UserRatings.length
    ,并且我的所有项都有10到20个评级。也许可以提供更多的上下文:
    spot
    是一个原始的JSON文件,然后我基于
    spot
    构建
    spotFirebase
    。我需要有不同的命名,一些其他类型的属性(如
    GeoPoint
    ),以及这个评级集合。我添加了填充方式。在这里,您可以获得一个示例文件: