我为预定义的类ParseInstallation创建了一个解析云beforeSave触发器,以便为同一个用户删除重复的解析安装,有点像this gist上的内容。我部署的云代码如下:
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.Installation);
//PROBLEM: request.object.get("username") is undefined...
query.equalTo("username", request.object.get("username"));
query.first().then(function(duplicate) {
if (typeof duplicate === "undefined") {
console.log("Duplicate does not exist, New installation");
response.success();
} else {
console.log("Duplicate exist..Trying to delete " + duplicate.id);
duplicate.destroy().then(function(duplicate) {
console.log("Successfully deleted duplicate");
response.success();
}, function() {
console.log(error.code + " " + error.message);
response.success();
});
}
}, function(error) {
console.warn(error.code + error.message);
response.success();
});
})
问题,因为它在代码中内联注释,是request.object.get("username")
返回undefined。事实上,request.object
的所有属性都是未定义的!
在我的Android客户端应用程序上,我将ParseUser和用户名关联到ParseInstallation对象,如下所示:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("username", username);
installation.put("user", user);
installation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null) {
Log.e(TAG, "Parse installation was created successfully!");
} else {
Log.e(TAG, "An error occurred while saving Parse installation: " + e.toString());
}
}
});
//more code...
} else {
Log.e(TAG, "Signup error: " + e.getMessage());
//more code...
}
}
});
所以,鉴于上述情况,我们可以预期,在解析云上,request.object.get("username")
将返回实际用户的用户名而不是未定义,正确??? (顺便说一句,是的,我已经检查过android客户端代码上的用户名变量不是空的也不是空的!)
为什么地狱request.object
属性都未定义?
如何从云触发器上的ParseInstallation获取username
然后???
此外,是否有人从IOS客户端应用程序中遇到此问题?
答案 0 :(得分:1)
通过request.user
对象检索用户名:
var User = request.user;
console.log(User.get("username"));
//or skip the first step:
console.log(request.user.get("username"));
注意,您也可以通过调用Parse.Object的attributes
属性来检索这些值(通常是不好的做法):
console.log(request.user.attributes.username);