Order by results in "is not a function"

1,215 views
Skip to first unread message

endor...@gmail.com

unread,
Jun 10, 2016, 11:04:18 AM6/10/16
to Dexie.js
Hi everyone,
i am new to DEXIE and everything works perfect until i want to
order my result.

This is my code:

db.playlists
.orderBy('widPosition')
.where('widStatus')
.equals('true').

the widStatus is saved as string. I if remove the "orderBy" line
from the code, it works. I can make DEXIE show me those entries where
the widStatus either is true or false.

But as soon as i activate the orderBy, it does not work anymore.
Can anyone point me to what i am missing? Where is the error?

Thanks in advance and greetings!
Carlos

David Fahlander

unread,
Jun 10, 2016, 11:10:30 AM6/10/16
to Dexie.js, endor...@gmail.com
orderBy() returns a Collection and the Collection doesn't have where()

You could do filter() instead of where() if you want the db to sort the results. See API reference and specifically Collection.filter().

David Fahlander

unread,
Jun 10, 2016, 11:13:01 AM6/10/16
to Dexie.js, endor...@gmail.com
Another option is to do Table.where() and use sortBy() instead of orderBy() at the end.

endor...@gmail.com

unread,
Jun 10, 2016, 2:21:54 PM6/10/16
to Dexie.js, endor...@gmail.com
Thanks. I will try that. Let me take the opportunity to thank you for Dexie.
I still need to learn about the differences between collections and tables.
To learn it i will try both of your solutions. Thanks again!

BTW: i am doing a Chrome app with the framework7 framework and the content is based on Dexie. Can i put your logo inside it together with "Database by Dexie"

Thanks and greeting!

endor...@gmail.com

unread,
Jun 10, 2016, 3:06:55 PM6/10/16
to Dexie.js, endor...@gmail.com
On Friday, June 10, 2016 at 4:13:01 PM UTC+1, David Fahlander wrote:
> Another option is to do Table.where() and use sortBy() instead of orderBy() at the end.

Can you direct me where i am going wrong with my code?

db.playlists


.where('widStatus')
.equals('true')

.sortBy('widPosition')
.each(function(oneItem){.....

endor...@gmail.com

unread,
Jun 10, 2016, 3:17:47 PM6/10/16
to Dexie.js, endor...@gmail.com

[SOLVED]
After the each i had a "finally()".
I found out that i have to put the sortBy at the very end after the finally.
Just like David said.
Thanks so much!

David Fahlander

unread,
Jun 10, 2016, 4:34:53 PM6/10/16
to Dexie.js
sortBy() returns a Promise of an array. You can't use each() on a promise but you can do forEach on the resulting array.

David Fahlander

unread,
Jun 10, 2016, 4:54:43 PM6/10/16
to Dexie.js

Yes you may endorse Dexie in your app. That's always nice.

I'm glad you like Dexie and I wish you good luck with your app! I would advice you to read the api reference for every method you are using because it's hard to guess what class is returned without reading it. I would also advice you to read about Promises to understand how to use them properly. There's a good blog post by Nolan Lawson that I can recommend, https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html.

Finally, if you're making a chrome app, you will be safe to use the yield keyword to consume promises. Read https://github.com/dfahlander/Dexie.js/wiki/Simplify-with-yield to learn more about that. It will simplify your code a lot.

Cheers, /D

Carlos Pinto

unread,
Jun 18, 2016, 12:55:29 PM6/18/16
to Dexie.js
sorry for disturbing with this subject again. I searched for any "foreeach" example on the net but could not find it. And within the Dexie samples i could not
find an example showing the correct usage. In theory i understand what you explained to me and i do love the promise solution that Dexie uses.
But i can not find out where to use the foreach instead of the Dexie each() solution.
This is my code a bit shortend:

var dataPlaylist= new Array();
db.playlists
.where('widStatus')
.equals('true')
.each(function(oneItem){
            var itemTemp = {
                "dataplID" : oneItem.plID,
                "dataWidID" : oneItem.widID,
                "dataDuration" : oneItem.widDuration,
                "dataPos" : oneItem.widPosition,
            };  
            dataPlaylist.push(itemTemp); 
            slideNum++;
        })
        .finally(function() {
            data = dataPlaylist;
            for(var i in data){
                var idd = data[i].dataDuration;
                //console.log('time is:'+idd);
            }
        })
        .sortBy('widPosition');


What i am trying to is to use the widPosition to sort.
I can not use the index because that is used as reference to be deleted.
The widPosition is saved as integer.

Thanks in advance for your time and support!
Reply all
Reply to author
Forward
0 new messages