0

如果现有 Parse 对象,我想有一个副本,然后进行一些编辑并将其保存为新的 Parse 对象,而不是手动设置每个字段。

这是我的云功能:

Parse.Cloud.define("SharePost", function(request, response) {
    var ShareUserID=request.params.ShareUserID;
    var UserID=request.params.UserID;
    var PostID=request.params.PostID;
    Parse.Cloud.useMasterKey();
    var user = new Parse.User({id:UserID});
    var shareuser = new Parse.User({id:ShareUserID});
    var query = new Parse.Query("Feed");
    query.get(PostID, {
    success: function(post) {
      var Post = Parse.Object.extend("Feed");
      var newpost = new Post()
      // here I would like to get the same object and make some edits o, it
      post.save( {
            success:function () {
                 response.success("Success");
            },
            error:function (pointAward, error) {
                 response.success(error);
            }
        }
    );    
    },
    error: function(error) {
      console.error("Got an error " + error.code + " : " + error.message);
    }
  });
}); 
4

1 回答 1

1

可能有一种更漂亮的方法,但一种肯定会在不依赖任何微妙之处的情况下工作的方法是:

function pfClone(fromObject, toObject, keys) {
    var _ = require('underscore');
    _.each(keys, function(key) {
        toObject.set(key, fromObject.get(key));
    });
}

像这样称呼它:

// after fetching a post called "post"
var Post = Parse.Object.extend("Feed");
var newpost = new Post();
var keys = ["title", "author" /* ...the keys you want to copy unchanged */ ];
pfClone(post, newpost, keys);
// change other properties of newpost here

甚至更漂亮的版本会自省传递的对象,然后构建和初始化克隆。这些想法中的任何一个的一个不雅之处是(上次我检查)PFObject 不允许您自省键,因此您被困在传递一组键。

于 2016-02-11T20:29:13.027 回答