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.

Slice in populate throws error

See original GitHub issue

I have two collection, a cart, and product Cart has an array of items with references to products The product has a prices array.

I want to do a query where I get the cart, and populate the products, but with the prices array sliced. I use the following query:

Cart.findById( id )
.populate({
    path:'items.products',
    options: {
        slice: {
            'prices': 1,
        }
    }
})

But it throws this error:

Error: slice() must be used after where() when called with these arguments

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
tsabolovcommented, Mar 19, 2015

I have a similar task: We have a collection of messages. Messages can have replies which are messages too. Only last reply (if any) should be returned with each message. I’ve tried to make a population like this:

query.populate({
  path: 'replies',
  options: {
    sort: { 'createdAt': -1 },
    limit: 1
  }
});

It works, but I’ve got another problem. I also need a total number of replies which is the length of replies array. I have a virtual property to get that. The thing is that after population replies.length becomes 1.

0reactions
nickydonnacommented, Mar 23, 2015

Great, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Why slice() is showing error but code run perfectly?
P.S.. This error will be shown first 4 times when i have total 6 values inside items. items is having object array. when...
Read more >
Mongoose v6.8.2: API docs
If bufferCommands is true, Mongoose will throw an error after bufferTimeoutMS if the operation is still buffered. [options.dbName] «String» The name of the ......
Read more >
Understanding Arrays and Slices in Go - DigitalOcean
This article will cover the array and slice data structures in the Go Programming language, which will provide you with the necessary ...
Read more >
The Basics of Indexing and Slicing Python Lists | by Joseph H
“Slicing” means getting a subset of elements from an iterable based on ... the slicer stops before populating any value into the slice....
Read more >
Salesforce CPQ error "Cannot read property 'slice' of null ...
When checking Renewal Quoted in a contract, an error appears: Cannot read property 'slice' of null. This error occurs when the Renewal Opportunity...
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