【问题标题】:Firestore set function not workingFirestore 设置功能不起作用
【发布时间】:2018-01-20 19:58:05
【问题描述】:

我正在尝试使用云功能将我的所有帖子移动到 Firestore

exports.copyPosts = functions.https.onRequest((req, res) => {
    var i = 0;
    var username;
    db.ref("feeds").child("all").limitToLast(2000).once("value", function (postSnap) {
            console.log(postSnap.numChildren());

        postSnap.forEach(function(topic){
                i = i + 1;


                    firestore.collection("topics").doc(i+"").set({
                        caption: topic.child("caption").val(),
                        time: topic.child("time").val(),
                        username: topic.child("username").val(),
                        category: topic.child("category").val(),
                        pic: topic.child("pic").val()

                    })

                if(postSnap.numChildren()==i){
                    res.contentType('application/json');
                    res.status(200).send("Success");
                }
            });
        });

});

返回成功。在此之前,我使用此循环将帖子复制到另一个实时数据库位置并且它有效。

但对于 Firestore,没有添加任何数据,也没有显示日志中的错误..

【问题讨论】:

  • 您是否尝试过在 forEach 中记录一些内容,看看它在做什么?您是否考虑过关注写入生成的承诺,并仅在它们全部解决后才允许函数终止?
  • 我同意@DougStevenson,对象的某些值可能未定义为set ({...})。还需要将标题、时间、用户名、类别和图片的双引号括起来,如果它们不是变量。

标签: javascript firebase google-cloud-functions google-cloud-firestore


【解决方案1】:

我可以使用云功能将数据插入到 Firestore。 您可以先将数据传递给变量,然后将变量放入 set() 操作中。希望对你有帮助

exports.insertDB = functions.https.onRequest((request,response) =>
{
  var firstname_input = request.query.name;
  db.collection("user").doc(firstname_input).set({
          first_name : firstname_input,
          last_name : "Chow",
          hobby : "Swimming"
        })
  response.send("Inserted a new user, name: " + firstname_input);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多