【发布时间】:2021-03-29 13:48:14
【问题描述】:
我有很多餐馆。我有另一个菜单集合。我正在使用云功能,并且在更新菜单时尝试更新餐厅集合中的文档。有时发生这种情况时,餐厅可能已被删除,因此我在尝试更新之前检查该文档是否存在。这是代码:
export const menuUpdated = functions.firestore.document('menus/{menuId}').onUpdate((change, context) => {
const menuId = context.params.menuId
const menuAfter = change.after.data()
const promises = []
const promise1 = updateRestaurant(menuAfter.restaurantId)
promises.push(promise1)
const promise2 = doSomethingElse()
promises.push(promise2)
return Promise.all(promises)
});
async function updateRestaurant(restaurantId: string) {
try {
const restaurant = await getRestaurant(restaurantId)
if (!restaurant) {
return true
}
const ref = db.collection('restaurants/').doc(restaurantId)
const updateObject = { 'name': 'hello'}
await ref.update(updateObject)
return true
} catch (error) {
functions.logger.error(error.message)
return false
}
}
async function getRestaurant(restaurantId: string) {
const ref = db.collection('restaurants').doc(restaurantId)
const doc = await ref.get()
if (!doc.exists) {
return null
}
const restaurant = doc.data()
restaurant.documentId = doc.id
return restaurant
}
这通常效果很好,但有时我会看到下面这样的错误。这就是为什么我在更新之前添加了检查文档是否存在但我仍然看到此错误的原因:
Error: 5 NOT_FOUND: No document to update: projects/XXX/databases/(default)/documents/restaurants/rClGJTKgHq3wv9SDJSDp
at Object.callErrorFromStatus (/workspace/node_modules/firebase-admin/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
at Object.onReceiveStatus (/workspace/node_modules/firebase-admin/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
at Object.onReceiveStatus (/workspace/node_modules/firebase-admin/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:336:141)
at Object.onReceiveStatus (/workspace/node_modules/firebase-admin/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181)
at process.nextTick (/workspace/node_modules/firebase-admin/node_modules/@grpc/grpc-js/build/src/call-stream.js:145:78)
at process._tickCallback (internal/process/next_tick.js:61:11)
Caused by: Error
at WriteBatch.commit (/workspace/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:413:23)
at DocumentReference.update (/workspace/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:388:14)
at updateRestaurant (/workspace/lib/index.js:1166:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
code: 5,
details:
'No document to update: projects/XXX/databases/(default)/documents/restaurants/rClGJTKgHq3wv9SDJSDp',
metadata: Metadata { internalRepr: Map {}, options: {} },
note:
'Exception occurred in retry method that was not classified as transient' }
你能看出我做错了什么吗?
【问题讨论】:
-
这获得了多少流量?餐厅是否有可能在您阅读后立即删除,然后再更新?
-
是的,这可能是问题所在,如果是这种情况,我是否可以忽略这些错误被记录?关于“重试方法中发生未归类为瞬态的异常”的注释呢?
-
你解决了吗?如果是,怎么做?
标签: node.js firebase google-cloud-firestore