i made 2 classes in parse user(by default) , userdata. beforesave trigger follows:
parse.cloud.beforesave(parse.user, function(request, response) { var userdataobject = new parse.object("userdata"); var fromuserpointer = {"__type":"pointer","classname":"_user","objectid":request.object.id}; return userdataobject.save({score: 0, ideascount: 0, followerscount:0, return:0}).then(function (userdata) { var userdatapointer = {"__type":"pointer","classname":"userdata","objectid":userdata.id}; request.object.set("userdata", userdatapointer); response.success(); }, function(error) { response.error(error.message); });
});
it saves user's userdata , takes userdata pointer field , saves in userdata class.
parse.cloud.aftersave(parse.user, function(request) { parse.cloud.usemasterkey(); var userpointer = {"__type":"pointer","classname":"_user","objectid":request.object.id}; var userdata = request.object.get("userdata"); if (userdata) { var userdatapointer = {"__type":"pointer","classname":"userdata","objectid":userdata.id}; var userdataquery = new parse.query("userdata"); userdata.set("user", userpointer); return userdata.save().then (function (userdata) { var activityobject = new parse.object("activity"); return activityobject.save({fromuserdata: userdatapointer, from:userpointer, touserdata:userdatapointer, to:userpointer, type:"follow"}); }).then (function (success) { }, function (error) { console.error("error in aftersave(user) : " + request.object.id + ":" + error.message); }); }
});
the problem that, before saving in user class, not create userdata, should create. hence these 2 triggers must run atomically, not.
Comments
Post a Comment