【发布时间】: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