1

inside my frontend and after authenticating a user, I have the following code that works: ..

...
.then(authedUser =>
        db
          .collection('comments')
          .find({}, { limit: 1 })
          .asArray()
      )
      .then(doc => console.log('doc:', doc)); // 1 doc returned as array, yes!

However, the following code doesn't work: .

...
.then(authedUser =>
        db
          .collection('comments')
          .find({})
          .limit(1)
          .asArray()
      )
      .then(doc => console.log('doc:', doc)); // error inside Promise, limit is not a function...

May I know why? I know that limit() is a cursor method and $limit is an aggregate stage, so now I am a bit confused.

Mauricio Maroto
  • 119
  • 1
  • 2
  • 11

2 Answers2

0

This is a little confusing in the docs because the second would work in a Stitch function but it doesn't work when using the SDK. The first is the correct way to do it from the SDK. In the SDK, a read operation doesn't have modifiers, which means you can't call .limit() on a find().

Here are the docs for what you're doing. Hope this helps!

haley
  • 1,165
  • 7
  • 13
0

The funn part is the second one is allowed in Mongoose as i acreated a Api on Node , there is the code , worked perfectly fine

/// get all the full list

app.get("/expenses" , async(req,res) => {
    const page = Number(req.query.page) || 1
    const limit = Number(req.query.limit) || 5
    const skip =  (page - 1) * limit
    try {
       const stu = await Expense.find({}).limit(limit).skip(skip) ;  empty means all 
       res.status(200).json(stu)
    }catch(err) {
        console.log(err)
        res.status(500).json({message:err.message})
    }
     })