How to get document id's in BaasBox[0.9.2] using java script

185 Views Asked by At

I'm new to BaasBox. Can i have answer for how to get all the document ID's in a collection of BaasBox using javascript.? Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

I have tried this code and i'm able to manage with it. Hope it may help someone.

BaasBox.loadCollection("Loyalties")
             .done(function(res) {
                 for (var i=0; i<res.length; i++){
                     if( res[i].email == "[email protected]")
                         console.log("result ", res[i].id);
                 }
            })
            .fail(function(error) {
              console.log("error ", error);
            })
0
On

I have been having problems doing this using the Baasbox Javascript SDK (loadCollectionWithParams()), so I use the following method in javascript using jquery:

var collectionname = 'mycollection';
var query = 'title = "godzilla"';
var url = 'http://localhost:9000/document/' + collectionname + '?where=' + encodeURIComponent(query);

$.ajax({
    url: url,
    method: 'GET',
    dataType: 'json'
}).done(function(res) {
    console.log( res );
}).fail(function(error) {
    console.log( error );
});

In your case, you want to filter by the email, so you can change the value of the query to something like:

var query = 'email = "[email protected]"';

Notice that for the query to work, 'title' and 'email' have to be first-level keys in your documents, in other words, they should look like:

{
    "title": "godzilla",
    "email": "[email protected]"
}

source: Baasbox Documentation