Jelajahi Sumber

Merge pull request #17816 from overleaf/bg-eslint-rule-for-find-with-await

add eslint rule for find with await

GitOrigin-RevId: 7e78104e610073ff3c151d7314753c8301a8c787
Brian Gough 2 tahun lalu
induk
melakukan
598eaf08fd

+ 5 - 0
services/web/.eslintrc

@@ -133,6 +133,11 @@
           {
             "selector": "CallExpression[callee.property.name='map'] Identifier[name='ObjectId']:first-child, CallExpression[callee.property.name='map'] MemberExpression[property.name='ObjectId']:first-child",
             "message": "Don't map ObjectId directly. Use `id => new ObjectId(id)` instead"
+          },
+          // Catch incorrect usage of `await db.collection.find()`
+          {
+            "selector": "AwaitExpression > CallExpression > MemberExpression[property.name='find'][object.object.name='db']",
+            "message": "Mongo find returns a cursor not a promise, use `for await (const result of cursor)` or `.toArray()` instead."
           }
         ]
       }

+ 1 - 1
services/web/scripts/split_tests_assigned_at_to_dates.js

@@ -2,7 +2,7 @@ const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
 
 async function updateStringDates() {
   await waitForDb()
-  const users = await db.users.find({
+  const users = db.users.find({
     splitTests: { $exists: true },
   })