简体   繁体   English

查询ParseObject

[英]Query on ParseObject

I'm trying to get an Object with a Parse query. 我正在尝试使用Parse查询获取Object。

This is my code : 这是我的代码:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> results, ParseException e) {
            if (e == null) {
                // Results were successfully found from the local datastore.
            } else {
                showLog(e.toString());
            }
        }
    });

I get this error: 我收到此错误:

com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for 'objectId'. com.parse.ParseException:java.lang.IllegalStateException:ParseObject没有'objectId'的数据。 Call fetchIfNeeded() to get the data. 调用fetchIfNeeded()来获取数据。

BTW my Conference Class contains Pointers. BTW我的Conference班包含指针。

If you're querying directly from Parse, you can do: 如果你直接从Parse查询,你可以这样做:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference");
...
query.include("name_of_the_column_containing_a_pointer");
query.include("another_pointer_column");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> results, ParseException e) {
        if (e == null) {
            // You can now access the pointers specified in the include
        } else {
            showLog(e.toString());
        }
    }
});

Otherwise, if you're querying the local datastore: 否则,如果您要查询本地数据存储区:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Conference");
...
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> results, ParseException e) {
        if (e == null) {
            ParseObject parseObject = results.get(0); // Get the object
            parseObject.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
                public void done(ParseObject result, ParseException e) {
                    if (e == null)
                        // Do something with result
                }
            }
        } else {
            showLog(e.toString());
        }
    }
});

Hi @Juergen I was able to get the objectId string using one of the following ways: 您好@Juergen我能够使用以下方法之一获取objectId字符串:

String id = ParseUser.getCurrentUser().getObjectId();

within the Findcallback done function. Findcallback done函数中。

A second way - also in the done() function of the findCallBack where the returned data is named 第二种方法 - 也在findCallBackdone()函数中,其中返回的数据被命名

objects.objects.get(0).getObjectId(); 

Here is a third way. 这是第三种方式。 The main key here is to set your ParseObject as a final variable so you can refer to it in your done() method. 这里的主要关键是将ParseObject设置为最终变量,以便您可以在done()方法中引用它。

final ParseObject po = new ParseObject("Test");
po.put("username", temp.getUserName());
po.saveInBackground(new SaveCallback() {
 public void done(ParseException e) {
  if (e == null) {
   String id = po.getObjectId();
   Log.d(TAG, "The object id is: " + id);
  } else { // The save failed. Log.d(TAG, "User update error: " + e); 
  }
 }
});

I hope this helps you. 我希望这可以帮助你。

I had the same problem. 我有同样的问题。 The problem was I overrided the getObjectId method in my ParseObject implementations: 问题是我在ParseObject实现中覆盖了getObjectId方法:

@ParseClassName("MyOwnParseClass")
public class MyOwnParseClassextends ParseObject {    
    public static ParseQuery<MyOwnParseClass> getQuery() {
        return ParseQuery.getQuery(MyOwnParseClass.class);
    }

    public String getObjectId() {
        return getString("objectId");
    }

    public String getName() {
        return getString("name");
    }

    public void setName(String name) {
        put("name", name);
    }
}

But ParseObject already has a getObjectId() method and using 但是ParseObject已经有了getObjectId()方法并且正在使用

return getString("objectId");

does not return the objectId . 不返回objectId

Try this code 试试这个代码

ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
        ParseUser user = ParseUser.getCurrentUser();
        query.whereEqualTo("owners", user);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, com.parse.ParseException e) {
                if (e == null) {
                    for (ParseObject parseObject : objects){
                        String task;
                        task= parseObject.get("owners").toString();
                        adapter.add(task);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    // Something went wrong.
                    Toast.makeText(getActivity(),"Error: " + e.getMessage().toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
ParseQuery<ParseObject> queryP = ParseQuery.getQuery("Courses");
    queryP.whereEqualTo("Student", nameStudent);
    queryP.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> coursesList, ParseException e) 
        {
            ArrayList<String> courses = null;
            if (e == null) 
            {
                courses = new ArrayList<String>();
                for (ParseObject course : coursesList) 
                {
                    String courseName = course.getString("CoursesNameInParseColumn");       
                    courses.add(courseName);
                }
            } 
            else 
            {
                Log.d("Post retrieval", "Error: " + e.getMessage());
            }

            populateCoursesList(courses);
        }
    });

try this out 试试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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