Reputation: 2045
When I want to retrieve ParseObjects (Posts) from parse db (mongo) to display in my Android app, I need to add new fields to ParseObject
in the cloud code before delivering to client. these fields are only necessary for the client and thus I do not want to save them to the cloud/db. but for some weird reason it seems like the additional fields are only delivered to client successfully if I save them to cloud.
something like this will work:
Parse.Cloud.define("getPosts", function(request, response){
const query = new Parse.Query("Post");
query.find()
.then((results) => {
results.forEach(result => {
result.set("cloudTestField", "this is a testing server cloud field");
});
return Parse.Object.saveAll(results);
})
.then((results) => {
response.success(results);
})
.catch(() => {
response.error("wasnt able to retrieve post parse objs");
});
});
this delivers all new fields to my client. but if I don't save them to db and just add them prior to client delivery something like this:
Parse.Cloud.define("getPosts", function(request, response){
const query = new Parse.Query("Post");
query.find()
.then((results) => {
results.forEach(result => {
result.set("cloudTestField", "this is a testing server cloud field");
});
response.success(results);
})
.catch(() => {
response.error("wasnt able to retrieve post parse objs");
});
});
Then for some reason, In my android studio (client log), I receive null on the key "cloudTestField"
ParseCloud.callFunctionInBackground("getPosts", params,
new FunctionCallback<List<ParseObject>>(){
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (objects.size() > 0 && e == null) {
for (ParseObject postObj : objects) {
Log.d("newField", postObj.getString("cloudTestField"));
}
} else if (objects.size() <= 0) {
Log.d("parseCloudResponse", "sorry man. no objects from server");
} else {
Log.d("parseCloudResponse", e.getMessage());
}
}
});
and for some reason, the output is:
newField: null
How do I add fields to ParseObjects in cloud without saving to db
Upvotes: 2
Views: 537
Reputation: 2045
Turnes out, you cannot add fields whom are not persistent - to ParseObject. So I needed to convert the parseObjects to Json and now it's working like a charm:
Parse.Cloud.define("getPosts", function(request, response){
const query = new Parse.Query("Post");
var postJsonList = [];
query.find()
.then((results) => {
results.forEach(result => {
var post = result.toJSON();
post.cloudTestField = "this is a testing server cloud field";
postJsonList.push(post);
});
response.success(postJsonList);
})
.catch(() => {
response.error("wasnt able to retrieve post parse objs");
});
});
Upvotes: 2