查询ParseObject

时间:2015-11-19 11:25:34

标签: java android parse-platform

我正在尝试使用Parse查询获取Object。

这是我的代码:

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());
            }
        }
    });

我收到此错误:

  

com.parse.ParseException:java.lang.IllegalStateException:ParseObject没有'objectId'的数据。调用fetchIfNeeded()来获取数据。

BTW我的Conference班级包含指针。

5 个答案:

答案 0 :(得分:9)

如果你直接从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());
        }
    }
});

否则,如果您要查询本地数据存储区:

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());
        }
    }
});

答案 1 :(得分:3)

您好@Juergen我能够使用以下方式之一获取String id = ParseUser.getCurrentUser().getObjectId(); 字符串:

Findcallback done

done()函数中。

第二种方式 - 也在findCallBack的{​​{1}}函数中,其中返回的数据被命名为

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

这是第三种方式。这里的主要关键是将您的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); 
  }
 }
});

我希望这会对你有所帮助。

答案 2 :(得分:2)

我遇到了同样的问题。问题是我在getObjectId实现中覆盖了ParseObject方法:

@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);
    }
}

但是ParseObject已经有getObjectId()方法并使用

return getString("objectId");

不会返回objectId

答案 3 :(得分:2)

试试此代码

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();
                }
            }
        });

答案 4 :(得分:2)

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);
        }
    });

试试这个