简体   繁体   English

带有 Promise 的 Firestore 循环

[英]Firestore Loops with Promises

I cannot figure out what is wrong with how I'm implementing promises inside loops in a Firebase function.我无法弄清楚我如何在 Firebase function 中的循环内实现承诺有什么问题。 I get this error when running this function as a Firebase Scheduled function: "TypeError: undefined is not a function" specifying line 202 in my index.js (below).运行此 function 作为 Firebase 计划的 function 时出现此错误:“TypeError:我的 index.jsbe 中未定义的函数”指定行。202 The function is not running to completion (all of the .then s are not completing). function 没有运行完成(所有.then都没有完成)。 I have two other concerns:我还有两个担忧:

  1. That I'm not using Promise.all correctly and not returning promises in the proper places.我没有正确使用 Promise.all 并且没有在适当的地方返回承诺。

  2. I get this error in my Firebase logs: "Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string."我在 Firebase 日志中收到此错误:“参数“documentPath”的值不是有效的资源路径。路径必须是非空字符串。” when trying to get the document within the path in section 2 of the code below.尝试在下面代码的第 2 节中的路径中获取文档时。 The path is correct and is indeed strings as I have verified by inspecting the reference object.路径是正确的,并且确实是字符串,正如我通过检查参考 object 验证的那样。

     checkForTweetsToDelete: function(db) { /////// // 1. Get first collction - streamers let streamersRef = db.collection('streamers'); let streamersQuery = streamersRef.get().then(streamersSnap => { var promises = []; /////// // 2. Get settings for each streamer - Here is where I get a "Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string." streamersSnap.forEach(streamerDoc => { let tweetSettingsRef = db.collection('streamers').doc((streamerDoc.id.toString())).collection('TweetSettings').doc('Settings'); let settingsDoc = tweetSettingsRef.get().then(doc => { /////// // 3. Get all 'no' Tweets for this streamer let tweetsRef = db.collection('streamers').doc(doc.data().twitchId).collection('TwitterPosts'); let tweetsQuery = tweetsRef.where('deleted', '==', 'no').get().then(tweetsSnap => { var tweetPromises = []; tweetsSnap.forEach(tweetDoc => { ///////////// // 4. Delete Tweet const client = new Twitter({ subdomain: "api", // "api" is the default (change for other subdomains) version: "1.1", // version "1.1" is the default (change for other subdomains) consumer_key: APP_KEY, // from Twitter. consumer_secret: APP_SECRET, // from Twitter. access_token_key: USER_TOKEN, // from your User (oauth_token) access_token_secret: SECRET // from your User (oauth_token_secret) }); const user = client.post('statuses/destroy', { id: tweetDoc.id, trim_user: 1 }).then(twitterResponse => { /////// // 5. Write to DB that this has been deleted. let tweetRef = tweetsRef.doc(tweetDoc.id.toString()); let updatedDoc = tweetRef.set({ deleted: 'yes' }, {merge: true}).then(() => { return; }); return Promise.all([updatedDoc]); // 5 /////// }); // 4 ///////////// tweetPromises.push(user); }); return Promise.all(tweetPromises); }); return Promise.all([tweetsQuery]); // 3 /////// }); promises.push(settingsDoc); }); return Promise.all(promises); // 2 /////// }); return Promise.all([streamersQuery]); // 1 /////// }

And my index.js还有我的 index.js

exports.scheduledFunction = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
            console.log('This will run every 1 minutes!');

            var scheduled = require('./scheduledFunctions');
            if (admin.apps.length === 0 ) { admin.initializeApp(functions.config().firebase); }
            var db = admin.firestore();

            //scheduled.checkForTweetsToDelete(db);
            //return scheduled.checkForTweetsToDelete(db);
            return Promise.all( scheduled.checkForTweetsToDelete(db) ).then(function() { // Line 202
                console.log('Finished all promises!');
            });
        });

I removed most of the logic inside the .then s as well as the catch s for readability.为了便于阅读,我删除了.thencatch中的大部分逻辑。

EDIT:编辑:

This question was helpful in understanding how this all should work: Firebase (Angular) Promises with Loops in Loops and after studying it I think I'm doing it just as explained, but still unsuccessful.这个问题有助于理解这一切应该如何工作: Firebase (Angular) Promises with Loops in Loops并且在研究它之后,我认为我正在按照解释进行操作,但仍然没有成功。

Turns out I was calling the incorrect path elsewhere in my code.原来我在代码的其他地方调用了不正确的路径。 I was getting a value from the database then using it to call another reference path but that value was undefined.我从数据库中获取了一个值,然后使用它来调用另一个引用路径,但该值未定义。 After fixing that I was able to fix the other promise error by following Roamer-1888's advice.修复后,我能够按照 Roamer-1888 的建议修复其他 promise 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM