简体   繁体   English

仅在客户端的Parse Cloud代码中向ParseObjects添加新字段。 无需保存到数据库

[英]Adding new fields to ParseObjects in Parse Cloud code for client only. without saving to DB

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. 当我想从parse db(mongo)中检索ParseObjects(帖子)以显示在我的Android应用程序中时,我需要在交付给客户端之前在云代码中向ParseObject添加新字段。 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" 然后由于某种原因,在我的android studio(客户端日志)中,我在键“ cloudTestField”上收到了null

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 如何在不保存到数据库的情况下将字段添加到云中的ParseObjects

Turnes out, you cannot add fields whom are not persistent - to ParseObject. 事实证明,您不能将非持久性字段添加到ParseObject。 So I needed to convert the parseObjects to Json and now it's working like a charm: 所以我需要将parseObjects转换为Json,现在它就像一个咒语一样工作:

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

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

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