Node.js Firestore DB在启动云功能后未得到更新?
我有一个云函数,当某个文档被更新时,它会被更新。该功能正在正确触发 当我到达函数的末尾时,当我要向文档添加数据时,什么都没有发生 我就是这么做的,我只是不知道怎么了Node.js Firestore DB在启动云功能后未得到更新?,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个云函数,当某个文档被更新时,它会被更新。该功能正在正确触发 当我到达函数的末尾时,当我要向文档添加数据时,什么都没有发生 我就是这么做的,我只是不知道怎么了 const docRef = db.collection('collection_name').doc('doc_name'); let setDocRef = docRef.set({ variable_name: 'something', }); 我还想知道这是否与我的进口产品有关,但
const docRef = db.collection('collection_name').doc('doc_name');
let setDocRef = docRef.set({
variable_name: 'something',
});
我还想知道这是否与我的进口产品有关,但我认为它们也符合要求
const functions = require('firebase-functions');
const axios = require('axios');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
编辑:根据要求,这里是云功能的完整代码
exports.myFunctionName = functions.firestore
.document('docName').onUpdate((change, context) => {
const usernames = []; //a list of usernames which im not filling here
let topValueIds = [];
// retrive username from settings here
axios.get('an-endpoint').then(resp => {
// Get top Values
const topValues = []; //some filtered array
let topPicks = [];
usernames.forEach(usr => {
axios.get('endpoint').then(resp => {
let score = 0;
// Get top Values for person being queried
const matchValues; // a sliced array
matchValues.forEach(value => {
if(topValueIds.includes(value.id)) {
score++;
}
});
if (score >= 1) {
topPicks.push(resp.data.person.publicId)
}
});
})
const docRef = db.collection('collection-name').doc('doc-name');
let setTopPicks = docRef.set({
user_list: topPicks,
});
});
您的云函数是一个后台触发函数,您调用了几个异步方法(从axios和Firebase),因此您需要正确链接这些方法返回的承诺并返回承诺链。更多细节请参见Firebase 以下更改应该可以做到这一点:
exports.myFunctionName = functions.firestore
.document('docName').onUpdate((change, context) => {
const usernames = []; //a list of usernames which I'm not filling here
let topValueIds = [];
return axios.get('an-endpoint')
.then(resp => {
// I guess you use the value of resp somewhere in this then() block
// But it is not clear where, from your question...
const topValues = []; //some filtered array
let topPicks = [];
const promises = [];
usernames.forEach(usr => {
// No use of the usr value??? You probably use it to define the URL called by axios...
promises.push(axios.get('endpoint'));
});
return Promises.all(promises);
})
.then(resultsArray => {
resultsArray.forEach(result => {
// .... Up to you to adapt this part!
// resultArray is an Array containing the results of all the promises passed to Promise.all(), i.e. the results of the axios calls
});
const docRef = db.collection('collection-name').doc('doc-name');
return docRef.set({
user_list: topPicks,
});
});
});
请注意,之所以使用,是因为您需要并行执行所有axios调用
另外,请注意,我允许您在第二个
forEach
循环中调整代码:resultArray
是一个数组,包含传递给Promise.all()
的所有承诺的结果,如中所述。您可以共享云函数的整个代码吗?这可能是由于CF的生命周期管理不正确导致的问题:您似乎没有返回set()
方法返回的承诺。@RenaudTarnec我刚刚返回了,谢谢您的帮助!您好@EstebanVargas,您是否有时间查看提议的解决方案?