繁体   English   中英

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

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

当我想从parse db(mongo)中检索ParseObjects(帖子)以显示在我的Android应用程序中时,我需要在交付给客户端之前在云代码中向ParseObject添加新字段。 这些字段需要在客户端,因此我不想将它们保存到云/分贝。 但是出于某些奇怪的原因,如果我将其他字段保存到云中,似乎其他字段只能成功交付给客户端。 这样的事情将工作:

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

这会将所有新字段交付给我的客户。 但是,如果我不将它们保存到数据库,而只是在客户端交付之前添加它们,例如:

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

然后由于某种原因,在我的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());
                    }
                }
            });

由于某种原因,输出为:

newField: null

如何在不保存到数据库的情况下将字段添加到云中的ParseObjects

事实证明,您不能将非持久性字段添加到ParseObject。 所以我需要将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