INVALID_ARGUMENT:每个请求最多允许500次写入-Firebase Cloud Function-Fire



我正试图在Firebase Cloud Function中运行以下代码。我要做的是循环遍历名为savedData的Firestore集合中的所有文档,解析每个文档中存储为字符串的JSON,然后将解析后的数据存储到名为stgPicks的集合中的新文档中。savedData集合中的每个文档都应该在stgPicks集合中创建50-100个新文档。

当我尝试运行该函数时,我会得到以下错误代码。

有人能帮助调试这个代码吗?我不确定我是否正确处理了这批货。

Error: 3 INVALID_ARGUMENT: maximum 500 writes allowed per request
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:175:52)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:341:141)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:304:181)
at Http2CallStream.outputStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:115:74)
at Http2CallStream.maybeOutputStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:154:22)
at Http2CallStream.endCall (/workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:140:18)
at Http2CallStream.handleTrailers (/workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:265:14)
at ClientHttp2Stream.emit (events.js:198:13)
at ClientHttp2Stream.EventEmitter.emit (domain.js:466:23)
exports.parsePicksRecover = functions.https.onRequest((req, res) => {
let savedDataRef = admin.firestore().collection('savedData')
let allDrafts = savedDataRef
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
const docId = doc.id
const getDoc = admin
.firestore()
.collection('savedData')
.doc(`${docId}`)
.get()
.then((doc) => {
if (!doc.exists) {
console.log('No matching document.')
} else {
const response = doc.data().data_json.replace(`,]`, `]`).replace(`,}`, `}`)
const data = JSON.parse(response)
const draft = data.draft
const draftId = draft.draftid
const draftYear = docId.substring(0, 4)
let paramsStatic = {
draftid: draftId,
datetime: draft.datetime,
myteam: draft.myteam,
user: draft.user,
draftyear: draftYear
}
const batchArray = []
const batch = admin.firestore().batch()
batchArray.push(batch)
let operationCounter = 0
let batchIndex = 0
draft.picks.forEach((data) => {
let paramsVar = {
teamid: data.teamid,
teamshortname: data.teamshortname,
fullname: data.fullname,
position: data.position,
school: data.school,
round: data.round,
ovrpick: data.ovrpick,
rdpick: data.rdpick,
createDate: admin.firestore.FieldValue.serverTimestamp()
}
let params = { ...paramsStatic, ...paramsVar }
let writeResult = admin
.firestore()
.collection('stgPicks')
.doc(`${draftYear}|${draftId}|${data.ovrpick}`)
batchArray[batchIndex].set(writeResult, params)
operationCounter++
if (operationCounter === 499) {
batchArray.push(firestore.batch())
batchIndex++
operationCounter = 0
}
})
batchArray.forEach(async (batch) => await batch.commit())
return
}
})
.catch((err) => {
console.log('Error getting document', err)
})
})
})
.catch((err) => {
console.log('Error getting documents', err)
})
})

Firebase错误日志

我使用PHP SDK确实遇到了同样的错误,在我的情况下,问题是在提交之后,我需要创建一个新的batch实例,否则我认为它会尝试提交两次旧文档更改,这就是为什么每次请求最多允许500次写入错误的原因,例如:

let batch = admin.firestore().batch();
for (i = 1; i <= 10000; ++i) {
// do something with the batch

if (0 === i % 500) {
batch.commit();
batch = admin.firestore().batch();
}
}

batch.commit();

相关内容

  • 没有找到相关文章

最新更新