pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
21k stars 1.02k forks source link

Sort error, pouchdb-find/mango "unknown operator" #164

Closed natew closed 7 years ago

natew commented 7 years ago

Apologies for opening all these issues!

Case

https://www.webpackbin.com/bins/-KjpgcHUKURB4JtitaYe

Code

import React from 'react'
import {render} from 'react-dom'
import 'base64-js'
import 'ieee754'
import 'isarray'
import 'rxjs'
import * as RxDB from 'rxdb'
import pIDB from 'pouchdb-adapter-idb'

RxDB.plugin(pIDB)

render(<div>see console</div>,document.querySelector('#app') )

RxDB.create({
  adapter: 'idb',
  name: 'user',
  password: 'password'
})
.then(db => db.collection({
  name: 'test3',
  schema: { title: 'test3', version: 0, properties: { name: { type: 'string', index: true } } }
}))
.then(collection => {
  console.log('got', collection)
  collection.insert({ name: '1233'  })
  collection.insert({ name: '12344'  })
  collection.find({ sort: ['name'] }).exec().then(console.log)
})

Error:

Uncaught (in promise) Error: unknown operator "0" - should be one of $eq, $lte, $lt, $gt, $gte, $exists, $ne, $in, $nin, $size, $mod, $regex, $elemMatch, $type or $all
    at match (https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17732:11)
    at https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17707:12
    at Array.every (native)
    at matchSelector (https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17705:31)
    at https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17691:12
    at Array.every (native)
    at rowFilter (https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17679:25)
    at https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17656:12
    at Array.filter (native)
    at Object.filterInMemoryFields (https://cdn.jsdelivr.net/webpack/v2/base64-js%401.2.0%2Bieee754%401.1.8%2Bi…om%4015.4.2%2Breact%4015.4.2%2Brxdb%404.0.0%2Brxjs%405.4.0/dll.js:17655:15)
natew commented 7 years ago

I know the chained .sort() can be used, but the object notation is actually really helpful for our stack. It lets you parameterize arguments and cache things, as well as gives more flexible api's to your functions (can pass in an object rather than some sort of cb + chain like query => query.sort()...)

pubkey commented 7 years ago

This is a bug. Fix will come. Please note that RxCollection.insert is an async function. If you do

  collection.insert({ name: '1233'  })
  collection.insert({ name: '12344'  })
  collection.find({ sort: ['name'] }).exec().then(console.log)

then the query will probably not find the two inserted documents. (depending on which storage-engine you use)

pubkey commented 7 years ago

I could not reproduce this at the test. I don't know if it's an RxDB issue. Reopen if you have more information.

natew commented 7 years ago

I still see this btw on the webpackbin: https://www.webpackbin.com/bins/-KjpgcHUKURB4JtitaYe, open the console and I get this error:

image

pubkey commented 7 years ago

Ok i found the issue. As described in the docs, RxCollection.find() only takes the selector, not the conditions. You cannot pass sort here. Use the sort-function.

natew commented 7 years ago

Actually, just looked at that doc page and it's not mentioned. Want me to add it?