question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

collection.find(...).toArray is not a function?

See original GitHub issue

I’ve implemented the example code, however when the resolver hits the data source, this error pops up

TypeError: collection.find(...).toArray is not a function
      at _dataloader.default.ids (E:\test-graphql\node_modules\apollo-datasource-mongodb\dist\cache.js:31:6)    
      at dispatchQueueBatch (E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:246:22)
      at dispatchQueue (E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:233:5)
      at E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:70:20
      at process._tickCallback (internal/process/next_tick.js:61:11)

This is how I create the data source in it’s own file

const { MongoDataSource } = require("apollo-datasource-mongodb");

class ProductPages extends MongoDataSource {
  getProductPage(id) {
    return this.findOneById(id);
  }
}

exports.ProductPagesDataSource = new ProductPages(ProductPageModel);

This is how I add it to the ApolloServer

const { ProductPagesDataSource } = require("../graphql/schema/schema");
const app = new ApolloServer({
  ...
  dataSources: () => ({
    productPages: ProductPagesDataSource
  })
});

And in the resolver I call it like this (I use graphql-compose with graphql-compose-mongoose for my schema, but that shouldn’t be an issue since I can make it work using the DataLoader package)

schemaComposer.Query.addFields({
  page: ProductPageSchema.get("$findById").wrapResolve(next => rp => {
    return rp.context.dataSources.productPages.getProductPage(rp.args._id);
  })
});

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lorensrcommented, Nov 30, 2019

Ah, thanks—I fixed the test on latest master

On Fri, Nov 29, 2019 at 3:33 AM Jimi notifications@github.com wrote:

I’ve run that test, and this is the output

PASS src/tests/cache.test.js

FAIL src/tests/datasource.test.js (7.238s)

● Mongoose › data source

TypeError: Cannot read property '_id' of null



  79 |     const users = new Users(UserModel)

  80 |     users.initialize()

> 81 |     const user = await users.findOneById(alice._id)

     |                                                ^

  82 |     expect(user.name).toBe('Alice')

  83 |   })

  84 |



  at Object._id (src/__tests__/datasource.test.js:81:48)

● Mongoose › collection

TypeError: Cannot read property '_id' of null



  86 |     const users = new Users(userCollection)

  87 |     users.initialize()

> 88 |     const user = await users.findOneById(alice._id)

     |                                                ^

  89 |     expect(user.name).toBe('Alice')

  90 |   })

  91 | })



  at Object._id (src/__tests__/datasource.test.js:88:48)

Test Suites: 1 failed, 1 passed, 2 total

Tests: 2 failed, 11 passed, 13 total

Snapshots: 0 total

Time: 7.796s

Ran all test suites.

The “test” database has been created successfully, along with the user document in it

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/8?email_source=notifications&email_token=AAB5LGGTSZM6MVUYWUODUM3QWDHV7A5CNFSM4JRVVJH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFOG2NY#issuecomment-559705399, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB5LGFV2Z3H4KXRXIRBVA3QWDHV7ANCNFSM4JRVVJHQ .

0reactions
lorensrcommented, Apr 13, 2020

Not sure why, but if you can post a small reproduction repo, I can take a look ☺️

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve 'TypeError: db.find(...).toArray is not a function' in ...
toArray to loop through all the documents. Whenever I run the code below, it shows TypeError: meme.find(...).toArray is not a function .
Read more >
How to solve 'TypeError: db.find(…).toArray is not a function' in ...
The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver. The find method in MongooseJS returns a Query object....
Read more >
"cursor.toArray is not a function""collection.find() accepts at ...
I am new to node js and currently i am practicing mongoDB via mongoose 6.0. Here i am getting this error on find...
Read more >
Node.js MongoDB Find - W3Schools
To select data from a collection in MongoDB, we can use the findOne() method. ... toArray(function(err, result) { if (err) throw err; console.log(result);...
Read more >
cursor.toArray() — MongoDB Manual
This page documents the mongo shell method, and does not refer to the MongoDB ... that applies toArray() to the cursor returned from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found