Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query database? #11

Closed
iahvector opened this issue May 11, 2015 · 13 comments
Closed

How to query database? #11

iahvector opened this issue May 11, 2015 · 13 comments
Labels

Comments

@iahvector
Copy link

how can I call collection.find() on android? meteor.call("/Collection/find") gives 404 despite that it works on the web client.

@ocram ocram added the question label May 11, 2015
@ocram
Copy link
Contributor

ocram commented May 11, 2015

Can you make sure the collection does actually exist and that your syntax is correct?

The call(...) method is used just like this in the source code of this library itself:

call("/"+collectionName+"/remove", new Object[] { query }, listener);

Moreover, the examples have a call to MongoDB's db.collection.count() implemented with this library's call(...) method:

@iahvector
Copy link
Author

This is my code:

if (meteor.isConnected()) {
    meteor.loginWithEmail(email, password, new ResultListener() {
        @Override
        public void onSuccess(String result) {
            Log.d(TAG, "Logged in: " + result);

            try {
                JSONObject login = new JSONObject(result);

                String userId = login.getString("id");
                String token = login.getString("token");
                long expiry = login.getJSONObject("tokenExpires").getLong("$date");

                Map<String, Object> user = new HashMap<String, Object>();
                user.put("_id", userId);

                Object[] queryParams = {user};

                meteor.call("/Users/find", queryParams, new ResultListener() {
                    @Override
                    public void onSuccess(String result) {
                        Log.d(TAG, "Call result: " + result);
                    }

                    @Override
                    public void onError(String error, String reason, String details) {
                        Log.d(TAG, "Error: " + error + " " + reason + " " + details);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String error, String reason, String details) {
            Log.d(TAG, "Error: " + error + " " + reason + " " + details);
        }
    });
}

@ocram
Copy link
Contributor

ocram commented May 14, 2015

Thanks for sharing the code!

There's nothing wrong with this library. It's just that Meteor doesn't offer find to be called directly. The only methods you can call on a collection are insert, update and remove. And for those, there are dedicated methods in this library so you don't need to use call(...).

Conceptually, retrieving data via find is not how Meteor works. In Meteor, you subscribe to data and the server pushes all changes to you via the callbacks of this library.

So instead of writing meteor.call("/Users/find", ...), you have to publish the data on the server and subscribe on the client with this library.

@ocram ocram closed this as completed May 14, 2015
@isdzulqor
Copy link

how do you handle meteor.call("/Users/find", ...) in server side?
when we use meteor.call so we must make meteor.method in server, right.. but I still don't understand in client it needs return data and the name of the meteor.call is "/user/find".
Is the name in meteor.method must /user/find too?
and how to handle return data ? Is the return data is json type?
Could you give some example please,,,
Thank you..

@iahvector
Copy link
Author

@isdzulqor as @mwaclawek said above, you can't use /Users/find. What I do is subscribe to the data I want and save them to a local database, practically replicating the functionality of MiniMongo

meteor.setCallback(new MeteorCallback() {

    // ...

    @Override
    public void onDataAdded(String collectionName, String documentID, String fieldsJson) {
        // if documentID exists in the local db, update with the new data
        // else insert the new data
    }

    // ...
});

@isdzulqor
Copy link

@iahvector so it needs SQLite in your android native, and it needs to parse the JSON?
Is it correct?

@iahvector
Copy link
Author

Yes, though not necessarily SQLite, you can use any DB available on android. I personally use SQLite with an ORM. Sugar ORM is the easiest, and I'm currently experimenting with FlowDB as it it provides broader functionality.

@isdzulqor
Copy link

@iahvector Thank you so much for sharing this, My english is still bad, so sometimes I feel hard to understanding the documentation of the library,, I will try that,,
Is it possible to upload file wtih this ddp library?

@iahvector
Copy link
Author

It can be, the guys at collectionFS do it, but I think it is complicated. When I needed to upload a file from android, I created a HTTP POST API for uploading files.

@isdzulqor
Copy link

HTTP POST API with restivus? you use Meteor for server or php when it needs to upload file?

@iahvector
Copy link
Author

I use meteor.

@isdzulqor
Copy link

@iahvector would you like to show me the code to make HTTP POST API to upload File like image? because I've implemented the tutorial from google but it's not working,

@ocram
Copy link
Contributor

ocram commented Dec 13, 2015

Thanks for the interesting discussion, @isdzulqor and @iahvector :)

But please try to open new issues for new problems and questions again :) The reason is that your questions and answer may really be helpful for other users and they may more easily find them if they are in separate issues.

Thank you!

@delight-im delight-im locked and limited conversation to collaborators Dec 13, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants