解析云code request.object.get(密钥)的回报总是不确定 [英] Parse cloud code request.object.get(key) return always undefined
问题描述
我试图解析上保存一个对象之前,做一个简单的检查。
我使用了beforeSave的方法来检查对象有正确必要的字段设置保存之前。
问题是,当我试图让我的对象的字段返还总是不确定的,即使它是设置正确地将在日志!
输入: {\"original\":null,\"update\":{\"ACL\":{\"abcdefghi\":{\"read\":true,\"write\":true}},\"isDeleted\":false,\"lastEdit\":0,\"name\":\"testobject\",\"uuid\":\"109d0b30-1ad5-408b-ba49-2ce024935476\"}}
和云code:
Parse.Cloud.beforeSave(MyObject的功能(请求,响应){
//总是返回以uuid错误打印
response.error(对象UUID:+ request.object.get(的uuid));
}
这是一个示例code,不是我的真code,但你可以看到传给这个函数的「uuid」字段内,但request.object.get(的「uuid」)对象始终返回未定义:
控制台日志:
结果:对象UUID:未定义
官方文件说,访问这种方式对象领域,所以这是一个解析错误或我做一些错误?
编辑1:
至于建议我tryied登录对象是这样的:
的console.log(对象:+ JSON.stringify(request.object));
的console.log(请求:+ JSON.stringify(要求));
的结果是:
对象:{}
要求:{对象:{}<一些其他领域..>}
任何想法?
编辑2:
我转载正确地将错误,这似乎是一个ACL的bug;
首先我创建延伸的parseObject一个新的对象(我工作在Android上):
@ParseClassName(人)
公共类ParsePerson扩展的parseObject { 公共无效setname可以(字符串名称){
把(名,名);
} 公共无效setAge(INT年龄){
把(时代,年龄);
} 公共字符串的getName(){
返回的getString(名称);
} 公众诠释getAge(){
返回调用getInt(时代);
}
}
和比我的后台线程它会创建并保存一个测试对象是这样的:
ParsePerson parsePerson =新ParsePerson();
parsePerson.setName(汤姆);
parsePerson.setAge(45);
尝试{
parsePerson.save();
}赶上(ParseException的E){
Debug.e(错误code:+ e.get code(),E);
}
比我上传的这个云code,它并没有别的比日志:
Parse.Cloud.beforeSave(人,功能(请求,响应){ //登录person对象
的console.log(对象:+ JSON.stringify(request.object)); //回应始终成功保存对象
response.success();
});Parse.Cloud.afterSave(人,功能(要求){ //再次登录person对象
的console.log(对象:+ JSON.stringify(request.object)); //成功应对
response.success();
});
运行它的工作原理,并正确保存在新表中的对象之后。日志:
输入:{原:空,更新:{时代:45,名:汤姆}}
结果:更新改为{时代:45,名:汤姆}
对象:{时代:45,名:汤姆}
我tryid做的最后一件事是ACL设置为此对象以这种方式编辑的Android code:
ParsePerson parsePerson =新ParsePerson();
parsePerson.setName(汤姆);
parsePerson.setAge(45);
parsePerson.setACL(新ParseACL(ParseUser.getCurrentUser())); // 新队
尝试{
parsePerson.save();
}赶上(ParseException的E){
Debug.e(错误code:+ e.get code(),E);
}
和运行一切后,这是日志:
输入:{原:空,更新:{ACL:{userAcl:{读:真实的,写:真正}}, 时代:45,名:汤姆}}
结果:更新改为{}
对象:{}
因此,这还不够吗?
编辑3:
我发现的唯一的解决方案(这更是一个解决办法)是从Android code保存对象没有ACL,然后设置ACL在beforeSave云code:
Parse.Cloud.beforeSave(人,功能(请求,响应){ //设置ACL此对象
VAR用户= request.user;
VAR newACL =新Parse.ACL();
newACL.setPublicReadAccess(假);
newACL.setPublicWriteAccess(假);
newACL.setReadAccess(user.id,真);
newACL.setWriteAccess(user.id,真);
request.object.setACL(newACL); //再次登录person对象
的console.log(对象:+ JSON.stringify(request.object));
}
我有一个非常类似的问题。我正在修改现有的项目,当我上载的code我开始错误的位置没有任何错误了以前去过。
我终于解开了它在.parse.project回到jssdk版本1.4.0
也许这也解决了你的问题。
i'm trying to make a simple check before saving an object on Parse. I'm using the "beforeSave" method to check if object has necessary fields setup correctly before saving.
The problem is that when i try to get a field of my object it return always undefined even if it is setup correcly in log!
Input: {"original":null,"update":{"ACL":{"abcdefghi":{"read":true,"write":true}},"isDeleted":false,"lastEdit":0,"name":"testobject","uuid":"109d0b30-1ad5-408b-ba49-2ce024935476"}}
and cloud code:
Parse.Cloud.beforeSave("MyObject", function (request, response) {
// return always an error with uuid printed
response.error("Object uuid: " + request.object.get("uuid"));
}
This is a sample code, not my real code but as you can see the object passed to this function has "uuid" field inside but request.object.get("uuid") return always "undefined":
Console log:
Result: Object uuid: undefined
Official documentation say to access object fields in this way, so is this a Parse bug or i'm doing some mistakes?
EDIT 1:
As suggested i tryied to log the object in this way:
console.log("Object: " + JSON.stringify(request.object));
console.log("Request: " + JSON.stringify(request));
The result is:
Object: {}
Request: {"object":{}, <some other fields..>}
Any idea?
EDIT 2:
I reproduced correcly the error and it seems to be a bug with ACL; First i created a new object extending ParseObject (i'm working on Android):
@ParseClassName("Person")
public class ParsePerson extends ParseObject {
public void setName(String name) {
put("name", name);
}
public void setAge(int age) {
put("age", age);
}
public String getName() {
return getString("name");
}
public int getAge() {
return getInt("age");
}
}
And than my background thread it will create and save a test object in this way:
ParsePerson parsePerson = new ParsePerson();
parsePerson.setName("Tom");
parsePerson.setAge(45);
try {
parsePerson.save();
} catch (ParseException e) {
Debug.e("Error code: "+ e.getCode(), e);
}
Than i uploaded this cloud code that does nothing else than log:
Parse.Cloud.beforeSave("Person", function (request, response) {
// log person object
console.log("Object: " + JSON.stringify(request.object));
// respond always success to save the object
response.success();
});
Parse.Cloud.afterSave("Person", function(request) {
// log person object again
console.log("Object: " + JSON.stringify(request.object));
// respond success
response.success();
});
After running it works and it saves correctly the object in the new table. Logs:
Input: {"original":null,"update":{"age":45,"name":"Tom"}}
Result: Update changed to {"age":45,"name":"Tom"}
Object: {"age":45,"name":"Tom"}
The last thing i tryid to do was to set ACL to this object editing android code in this way:
ParsePerson parsePerson = new ParsePerson();
parsePerson.setName("Tom");
parsePerson.setAge(45);
parsePerson.setACL(new ParseACL(ParseUser.getCurrentUser())); // new line
try {
parsePerson.save();
} catch (ParseException e) {
Debug.e("Error code: "+ e.getCode(), e);
}
And after running everything this is the log:
Input: {"original":null,"update":{"ACL":{"userAcl":{"read":true,"write":true}},"age":45,"name":"Tom"}}
Result: Update changed to {}
Object: {}
So is this enough?
EDIT 3:
The only solution i found (this is more a workaround) is to save the object without ACL from Android code and then set ACL in beforeSave on cloud code:
Parse.Cloud.beforeSave("Person", function (request, response) {
// setup ACL for this object
var user = request.user;
var newACL = new Parse.ACL();
newACL.setPublicReadAccess(false);
newACL.setPublicWriteAccess(false);
newACL.setReadAccess(user.id, true);
newACL.setWriteAccess(user.id, true);
request.object.setACL(newACL);
// log person object again
console.log("Object: " + JSON.stringify(request.object));
}
I had a very similar problem. I was making changes to an existing project and when I uploaded the code I started getting error where no errors had been before.
I finally solved it by going back to jssdk version to 1.4.0 in .parse.project
Maybe it solves your problem also.
这篇关于解析云code request.object.get(密钥)的回报总是不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!