这是我的任务。有名为过去,今天和未来的集合。今天的馆藏应该有4个文件。我需要执行以下操作:(1)将字段“ publishedNumber”中具有最小值的文档从集合“ today”移动到集合“过去”。 (2)在集合“ future”的“ next”字段中找到值为true的文档,如果没有该文档,则在“ suggestedDate”字段中找到具有特定值的文档,如果没有这样的文档,则取在“数字”字段中具有最小值的文档。 (3)将在第2步中找到的文档移动到“ today”集合中,并在其字段“ publishedDate”和“ publishedNumber”中添加某些值。
这是我的问题:
1)在我的函数中,我重复了步骤(3)的相同代码3次。有没有办法提取它并以某种方式重用?
2)部署功能时,我收到两个“警告,避免嵌套promises promise / no-nesting”。如何避免在这里嵌套承诺?
这是我第一次使用Firebase云功能和Javascript;因此,我们将不胜感激,感谢发现的任何错误以及重构代码以使其工作/看起来更好的任何建议和建议。
exports.publish = functions.https.onRequest((req, res) => {
var lastPublishedNumber;
const now = new Date().toISOString().substring(0,10);
let queryToday = firestore.collection("today").orderBy('publishedNumber');
let queryFutureByNext= firestore.collection("future").where('next', '=', true);
let queryFutureBySuggestedDate = firestore.collection("future").where('suggestedDate', '=', now);
let queryFutureOrderedBySuggestedNumber = firestore.collection('future').orderBy('number').limit(1);
const batch = firestore.batch();
return queryToday
.get()
.then(todaySnapshot => {
if (todaySnapshot.empty) {
let err = Error(`ERROR: NO documents ordered by publishedNumber in today`);
return Promise.reject(err);
}
let todayDocCount = todaySnapshot.size;
if (todayDocCount !== 4) {
let err = Error(`WRONG number of documents: ${todayDocCount} found in today`);
return Promise.reject(err);
}
//store last published number
const todayDocLast = todaySnapshot.docs[3];
lastPublishedNumber = todayDocLast.get(`publishedNumber`);
console.log(`last published number in today: ${lastPublishedNumber}`)
//first document needs to be moved to past
const todayDocFirst = todaySnapshot.docs[0];
const todayDocFirstData = todayDocFirst.data();
const todayToPastRef = firestore.collection('past').doc();
batch.set(todayToPastRef, todayDocFirstData);
batch.delete(todayDocFirst.ref);
return queryFutureByNext.get();
})
.then(futureByNextSnapshot => {
if (futureByNextSnapshot.empty) {
console.log("NO documents with next == true in future, will try suggestedDate");
return queryFutureBySuggestedDate
.get()
.then(futureBySuggestedDateSnapshot => {
if (futureBySuggestedDateSnapshot.empty) {
console.log("NO documents with suggestedDate == now in future, will take first ordered by number");
return queryFutureOrderedBySuggestedNumber
.get()
.then( futureOrderedBySuggestedNumberSnapshot => {
if (futureOrderedBySuggestedNumberSnapshot.empty) {
let err = Error(`NO documents with number found in future`);
return Promise.reject(err);
}
console.log(`needs to do all the same code_3`)
//----- same code_3 starts here----------
//document needs to be moved to today
const futureOrderesBySuggestedNumberDocFirst = futureOrderedBySuggestedNumberSnapshot.docs[0];
let futureOrderesBySuggestedNumberDocFirstID = futureOrderesBySuggestedNumberDocFirst.id;
var futureOrderesBySuggestedNumberDocFirstData = futureOrderesBySuggestedNumberDocFirst.data();
//adding publicationDate like 2019-07-16
console.log(`publication date: ${now}`)
const futureToTodayRef = firestore.collection('today').doc(`${futureOrderesBySuggestedNumberDocFirstID}`);
batch.set(futureToTodayRef, futureOrderesBySuggestedNumberDocFirstData);
batch.update(futureToTodayRef, {publishedDate: now});
batch.update(futureToTodayRef, {publishedNumber: lastPublishedNumber + 1});
batch.delete(futureOrderesBySuggestedNumberDocFirst.ref);
console.log("SUCCESS_3: we are ready to commit batch!");
return batch.commit();
//----- same code_3 ends here----------
})
}
let futureBySuggestedDateDocsCount = futureBySuggestedDateSnapshot.size;
if (futureBySuggestedDateDocsCount !== 1) {
let err = Error(`WRONG number of documents with suggested date: ${futureBySuggestedDateDocsCount} found in future`);
return Promise.reject(err);
}
//----- same code_2 starts here----------
console.log(`needs to do all the same code_2`)
//document needs to be moved to today
const futureBySuggestedDateDocFirst = futureBySuggestedDateSnapshot.docs[0];
let futureBySuggestedDateDocFirstDataID = futureBySuggestedDateDocFirst.id;
var futureBySuggestedDateDocFirstData = futureBySuggestedDateDocFirst.data();
//adding publicationDate like 2019-07-16
console.log(`publication date: ${now}`)
const futureToTodayRef = firestore.collection('today').doc(`${futureBySuggestedDateDocFirstDataID}`);
batch.set(futureToTodayRef, futureBySuggestedDateDocFirstData);
batch.update(futureToTodayRef, {publishedDate: now});
batch.update(futureToTodayRef, {publishedNumber: lastPublishedNumber + 1});
batch.delete(futureBySuggestedDateDocFirst.ref);
console.log("SUCCESS_2: we are ready to commit batch!");
return batch.commit();
//----- same code_2 ends here----------
});
}
let futureByNextDocsCount = futureByNextSnapshot.size;
if (futureByNextDocsCount !== 1) {
let err = Error(`WRONG number of documents with next: ${futureByNextDocsCount} found in future`);
return Promise.reject(err);
}
//----- same code_1 starts here----------
//document needs to be moved to today
const futureByNextDocFirst = futureByNextSnapshot.docs[0];
let futureByNextDocFirstDataID = futureByNextDocFirst.id;
var futureByNextDocFirstData = futureByNextDocFirst.data();
//adding publicationDate like 2019-07-16
console.log(`publication date: ${now}`)
const futureToTodayRef = firestore.collection('today').doc(`${futureByNextDocFirstDataID}`);
batch.set(futureToTodayRef, futureByNextDocFirstData);
batch.update(futureToTodayRef, {publishedDate: now});
batch.update(futureToTodayRef, {publishedNumber: lastPublishedNumber + 1});
batch.delete(futureByNextDocFirst.ref);
console.log("SUCCESS_1: we are ready to commit batch!");
return batch.commit();
//----- same code_1 ends here----------
})
.catch(err => {
return console.log(`Error handling in catch. ${err}`);
})
});