Firebase 函数执行耗时 2794 毫秒,完成状态为:'ok',...但 Firestore 中没有发生任何变化
Firebase Function execution took 2794 ms, finished with status: 'ok' ,... but no change took place in Firestore
我正在尝试使用 Cloud Functions 和 NodeJs 更新 Firestore 中嵌套子集合中的所有文档。
collection().doc().collection.doc().collection().doc()
/timeline/105358539742688769892/publisherId/105358539742688769892/posts/29b975f2-0d53-4e06-8365-3d5442355a78
这是我的代码。
exports.onCreateTimelineTest = functions.firestore
.document("/timelineTest/{docId}")
.onCreate(async (snapshot, context) => {
const timeNow =new Date();
const db = admin.firestore();
return db.collection("timeline")
.get()
.then((timelineQuerySnapshot) => {
timelineQuerySnapshot.forEach(async(timelineQuerySnapshot) => {
console.log("all the documnets inside timeline are", timelineQuerySnapshot.id);
await db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.get()
.then((publisherSnapshot)=>{
publisherSnapshot.forEach(async(publisherSnapshot)=>{
console.log("all the documnets inside timeline are", publisherSnapshot.id);
await db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.doc(publisherSnapshot.id)
.collection("posts")
.where('uploadTime', '<=', timeNow)
.get()
.then((postSnap)=>{
postSnap.forEach(async(postSnap)=>{
console.log("all the documnets inside timeline are", postSnap.id);
return db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.doc(publisherSnapshot.id)
.collection("posts")
.doc(postSnap.id)
.set({
'name': 'madhav'
}).catch((err)=>{
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
这些功能在模拟器中运行良好,部署后,状态为 'ok'。
但是当我检查 firestore 时,我发现没有发生任何变化。还是老样子
花了 2 天时间,我找到了解决方案,而且非常愚蠢。
在屏幕截图中它说,
此文档不存在,不会出现在查询和快照中。
所以,我不得不为每个文档添加一个字段,然后功能开始正常工作。
我正在尝试使用 Cloud Functions 和 NodeJs 更新 Firestore 中嵌套子集合中的所有文档。
collection().doc().collection.doc().collection().doc()
/timeline/105358539742688769892/publisherId/105358539742688769892/posts/29b975f2-0d53-4e06-8365-3d5442355a78
这是我的代码。
exports.onCreateTimelineTest = functions.firestore
.document("/timelineTest/{docId}")
.onCreate(async (snapshot, context) => {
const timeNow =new Date();
const db = admin.firestore();
return db.collection("timeline")
.get()
.then((timelineQuerySnapshot) => {
timelineQuerySnapshot.forEach(async(timelineQuerySnapshot) => {
console.log("all the documnets inside timeline are", timelineQuerySnapshot.id);
await db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.get()
.then((publisherSnapshot)=>{
publisherSnapshot.forEach(async(publisherSnapshot)=>{
console.log("all the documnets inside timeline are", publisherSnapshot.id);
await db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.doc(publisherSnapshot.id)
.collection("posts")
.where('uploadTime', '<=', timeNow)
.get()
.then((postSnap)=>{
postSnap.forEach(async(postSnap)=>{
console.log("all the documnets inside timeline are", postSnap.id);
return db.collection("timeline")
.doc(timelineQuerySnapshot.id)
.collection("publisherId")
.doc(publisherSnapshot.id)
.collection("posts")
.doc(postSnap.id)
.set({
'name': 'madhav'
}).catch((err)=>{
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
return null
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
});
});
这些功能在模拟器中运行良好,部署后,状态为 'ok'。 但是当我检查 firestore 时,我发现没有发生任何变化。还是老样子
花了 2 天时间,我找到了解决方案,而且非常愚蠢。
在屏幕截图中它说,
此文档不存在,不会出现在查询和快照中。
所以,我不得不为每个文档添加一个字段,然后功能开始正常工作。