Node.js 使用通配符路径don';行不通
我尝试使用云函数更新集合中的所有文档。使用此代码,它可以更新集合releasedQuestions中的一个文档(id为0):Node.js 使用通配符路径don';行不通,node.js,typescript,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Typescript,Firebase,Google Cloud Firestore,Google Cloud Functions,我尝试使用云函数更新集合中的所有文档。使用此代码,它可以更新集合releasedQuestions中的一个文档(id为0): exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => { const test = admin.firestore().doc('releasedQuestions/0').update({restDuration: 42})
exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => {
const test = admin.firestore().doc('releasedQuestions/0').update({restDuration: 42})
.then(snapshot => {
response.send(0)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
但当我将使用如下通配符路径更新集合中的所有文档时:
const test = admin.firestore().doc('releasedQuestions/{qid}').update({restDuration: 42})
它不起作用。有人能帮我吗
我的云Firestore结构如下:
您正在使用的通配符路径语法(即
doc('releasedQuestions/{qid}')
)只能在云函数定义中使用,例如,当您通过指定文档路径和事件类型来定义云Firestore触发器时,如下所示:
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {...});
exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => {
const db = admin.firestore();
db.collection('releasedQuestions').get()
.then(snapshot => {
let batch = db.batch();
snapshot.forEach(doc => {
batch.update(doc.ref, { restDuration: 42 });
});
return batch.commit()
})
.then(() => {
response.send(0)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
在您的情况下,您实际上是在调用该方法,documentPath
参数应为字符串
。这就是为什么它适用于第一种情况,但不适用于第二种情况(在您的收藏中没有任何ID为{qid}
的Firestore文档)
如果希望在HTTP云函数中更新集合的所有文档,可以使用,如下所示:
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {...});
exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => {
const db = admin.firestore();
db.collection('releasedQuestions').get()
.then(snapshot => {
let batch = db.batch();
snapshot.forEach(doc => {
batch.update(doc.ref, { restDuration: 42 });
});
return batch.commit()
})
.then(() => {
response.send(0)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
但是请注意,批处理写入最多可以包含500个操作。因此,如果集合包含的文档超过500个,则可以使用
作为旁注,值得注意的是 模板文字由反勾号括起(严重重音) 字符而不是双引号或单引号。。。可以容纳 占位符
您使用的通配符路径语法(即
doc('releasedQuestions/{qid}')
)只能在云函数定义中使用,例如,当您通过指定文档路径和事件类型定义云Firestore触发器时,如下所示:
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {...});
exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => {
const db = admin.firestore();
db.collection('releasedQuestions').get()
.then(snapshot => {
let batch = db.batch();
snapshot.forEach(doc => {
batch.update(doc.ref, { restDuration: 42 });
});
return batch.commit()
})
.then(() => {
response.send(0)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
在您的情况下,您实际上是在调用该方法,documentPath
参数应为字符串
。这就是为什么它适用于第一种情况,但不适用于第二种情况(在您的收藏中没有任何ID为{qid}
的Firestore文档)
如果希望在HTTP云函数中更新集合的所有文档,可以使用,如下所示:
exports.useWildcard = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {...});
exports.decreaseQuestionRestDuration = functions.https.onRequest((request, response) => {
const db = admin.firestore();
db.collection('releasedQuestions').get()
.then(snapshot => {
let batch = db.batch();
snapshot.forEach(doc => {
batch.update(doc.ref, { restDuration: 42 });
});
return batch.commit()
})
.then(() => {
response.send(0)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
});
但是请注意,批处理写入最多可以包含500个操作。因此,如果集合包含的文档超过500个,则可以使用
作为旁注,值得注意的是 模板文字由反勾号括起(严重重音) 字符而不是双引号或单引号。。。可以容纳 占位符