简体   繁体   中英

Parse “no results found for query (code 101)”. Can't get object id

I've installed the example parse server ( https://github.com/ParsePlatform/parse-server-example ) on my desktop and made a simple app to test it.

My app saves an object to the server, gets the object and sets mTextView's value to the value of my object.

The problem is, when I try this code to get data from server it works:

    query.getInBackground("5K7N8a8Dmd", new GetCallback<ParseObject>() ...

(got the object id with curl)

but when I try this (to get object id w/o using curl):

            gameScore.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    objectId = gameScore.getObjectId();
                } else {
                    Log.e("saveInBackground", getErrorMessage(e));
                }
            }
        });

...

    query.getInBackground(objectId, new GetCallback<ParseObject>() ...

it doesn't work.

logcat:

E/getInBackground﹕ no results found for query - code: 101

MainActivity.java

    public class MainActivity extends Activity {

    public TextView mTextView;
    public String objectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.text_view);

        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                        .applicationId(Constants.APP_ID)
                        .server(Constants.SERVER_URL)
                        .build()
        );

        final ParseObject gameScore = new ParseObject("Foo1234");
        gameScore.put("score", 5000);
        gameScore.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                if (e == null) {
                    objectId = gameScore.getObjectId();
                } else {
                    Log.e("saveInBackground", getErrorMessage(e));
                }
            }
        });

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");
        query.getInBackground(objectId, new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    mTextView.setText(Integer.toString(gameScore.getInt("score")));
                } else {
                    Log.e("getInBackground", getErrorMessage(e));
                }
            }
        });
    }

    public String getErrorMessage(ParseException e) {
        return e.getMessage() + " - code: " + e.getCode();
    }
}

Constants.java

    public class Constants {
    public static String SERVER_URL = "http://192.168.1.14:1337/parse/";
    public static String APP_ID = "myAppId";
}

Thanks in advance.

Try this

ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234");
query.whereEqualTo("objectId","wfBB0gpCkP");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> objects, ParseException e) {
        if (e == null) {
            // row of Object Id "wfBB0gpCkP"
        } else {
            // error
        }
    }
});

Also change

ParseQuery<ParseObject> query = ParseQuery.getQuery("Foo1234");

to

ParseQuery<ParseObject> query = ParseQuery.getQuery("_Foo1234"); 

See if this works.

The problem is the "background" part of saveInBackground() . The save isn't complete on the line following the save. In fact, it hasn't even begun to save. The save isn't complete until the completion handler runs.

Consider...

        // code here runs **first**
        gameScore.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            // code here runs **third, much later than second**
            if (e == null) {
                objectId = gameScore.getObjectId();
            } else {
                Log.e("saveInBackground", getErrorMessage(e));
            }
        }
        // code here runs **second, immediately**

Run the "get" query within the completion handler of the save and you'll see that it works. (Of course, there's not much reason to get the object since you have the handle to the object upon which you just invoked save).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM