GraphQLGuide / apollo-datasource-mongodb

Apollo data source for MongoDB
MIT License
285 stars 64 forks source link

TypeError: cursor.toArray is not a function Node js #77

Closed rawand121 closed 3 years ago

rawand121 commented 3 years ago

I'm using next.js and node.js so i want to pre-render a page and i sent a request from getServerSideProps method from Next.Js..

but i always get this error:

TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (D:\Next Js\book\node_modules\mongoose\lib\query.js:2152:19)
    at model.Query._wrappedThunk [as _find] (D:\Next Js\book\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
    at D:\Next Js\book\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (node:internal/process/task_queues:76:11)

D:\Next Js\book\node_modules\mongodb\lib\collection.js:238
            throw new error_1.MongoInvalidArgumentError('Method "collection.find()" accepts at most two arguments');
                  ^
MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
    at Collection.find (D:\Next Js\book\node_modules\mongodb\lib\collection.js:238:19)
    at NativeCollection.<computed> [as find] (D:\Next Js\book\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
    at NativeCollection.Collection.doQueue (D:\Next Js\book\node_modules\mongoose\lib\collection.js:135:23)
    at D:\Next Js\book\node_modules\mongoose\lib\collection.js:82:24
    at processTicksAndRejections (node:internal/process/task_queues:76:11)
npm ERR! code 1
npm ERR! path D:\Next Js\book
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c next start

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Rawan\AppData\Local\npm-cache\_logs\2021-09-07T11_13_16_514Z-debug.log

but im not getting error if first go to another URL that not send any http request for pre-render the page, then come back to first page that i want to send http request.

and here is my codes for Next.js

export default function Home(props) {
  return (
    <Layout title="Home">
      <div style={{ direction: "rtl" }}>
        <Header />
        <div className="container">
          <Newest latest={props.posts} />
          <Boxes />
        </div>
        <Footer />
      </div>
    </Layout>
  );
}

export const getServerSideProps = async ({ locale }) => {
  const res = await fetch("http://localhost:3000/api/latest");
  const data = await res.json();

  return {
    props: {
      language: locale,
      posts: data,
    },
  };
};

API

import nc from "next-connect";
import dbConfig from "../../Backend/config/dbConfig";
import { getLatest } from "../../Backend/controllers/books";
import onError from "../../Backend/middlewares/createError";
const handler = nc({ onError });

dbConfig();

handler.get(getLatest);

export default handler;

Node JS

const getLatest = async (req, res, next) => {
  try {
    const books = await booksModel.find().exec();
    res.status(200).json({
      books: [books[0], books[1], books[2], books[3], books[4]],
    });
  } catch (err) {
    console.log(err);
    next(new ErrorHandler(err.message, 500));
  }
};
lorensr commented 3 years ago

I don’t see anything in the code or stack traces that involve this package—perhaps it’s from Mongoose, or how you’re using Mongoose.

On Tue, Sep 7, 2021 at 7:54 AM Rawand121 @.***> wrote:

I'm using next.js and node.js so i want to pre-render a page and i sent a request from getServerSideProps method from Next.Js..

but i always get this error:

TypeError: cursor.toArray is not a function at model.Query. (D:\Next Js\book\node_modules\mongoose\lib\query.js:2152:19) at model.Query._wrappedThunk [as _find] (D:\Next Js\book\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8) at D:\Next Js\book\node_modules\kareem\index.js:370:33 at processTicksAndRejections (node:internal/process/task_queues:76:11)

D:\Next Js\book\node_modules\mongodb\lib\collection.js:238 throw new error_1.MongoInvalidArgumentError('Method "collection.find()" accepts at most two arguments'); ^ MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments at Collection.find (D:\Next Js\book\node_modules\mongodb\lib\collection.js:238:19) at NativeCollection. [as find] (D:\Next Js\book\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33) at NativeCollection.Collection.doQueue (D:\Next Js\book\node_modules\mongoose\lib\collection.js:135:23) at D:\Next Js\book\node_modules\mongoose\lib\collection.js:82:24 at processTicksAndRejections (node:internal/process/task_queues:76:11) npm ERR! code 1 npm ERR! path D:\Next Js\book npm ERR! command failed npm ERR! command C:\Windows\system32\cmd.exe /d /s /c next start

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Rawan\AppData\Local\npm-cache_logs\2021-09-07T11_13_16_514Z-debug.log

but im not getting error if first go to another URL that not send any http request for pre-render the page, then come back to first page that i want to send http request.

and here is my codes for Next.js

export default function Home(props) { return (

); }

export const getServerSideProps = async ({ locale }) => { const res = await fetch("http://localhost:3000/api/latest"); const data = await res.json();

return { props: { language: locale, posts: data, }, }; };

API

import nc from "next-connect"; import dbConfig from "../../Backend/config/dbConfig"; import { getLatest } from "../../Backend/controllers/books"; import onError from "../../Backend/middlewares/createError"; const handler = nc({ onError });

dbConfig();

handler.get(getLatest);

export default handler;

Node JS

const getLatest = async (req, res, next) => { try { const books = await booksModel.find().exec(); res.status(200).json({ books: [books[0], books[1], books[2], books[3], books[4]], }); } catch (err) { console.log(err); next(new ErrorHandler(err.message, 500)); } };

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/77, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB5LGABMIJ35AHLDOMMP7TUAX4PVANCNFSM5DSHAMEQ .

rawand121 commented 3 years ago

Thank You, i solved and the problem is that, Model was not initialized at sending Request and i fixed that with add dbConfig in controller file..