[英]Parse Cloud Code: Logic Branching in Promises
I'm trying to write a Parse.com Cloud Code function to accomplish the following workflow: 我试图编写一个Parse.com云代码功能来完成以下工作流程:
code
. code
对象。 code
is assumed to have a pointer to an object of type item
. code
的对象具有一个指向类型为item
的对象的指针。 code.item
is checked to see whether it has a pointer to an object of type alert
. code.item
以查看其是否具有指向alert
类型的对象的指针。 code.item.alert
does exist, then I want to fetch the full alert
object, including pointers which may or may not exist, up to 2 layers deep. code.item.alert
确实存在,那么我想获取完整的alert
对象,包括可能存在或可能不存在的指针(最多2层)。 As I begin writing the code for this function, I can get it working to the point of checking to see whether the code
exists and, if so, whether code.item.alert
also exists. 当我开始为该函数编写代码时,我可以使其工作到检查
code
是否存在以及是否存在code.item.alert
。
This is where the problem arises. 这就是问题所在。 As it currently stands, in the working version of my function, the
alert
item that is returned is only the class type and objectId. 就目前情况而言,在我的函数的工作版本中,返回的
alert
项仅是类类型和objectId。 I understand why that is happening, and I am trying to write code to populate the object before returning it, but I am failing in that attempt. 我了解为什么会发生这种情况,并且我尝试编写代码以在返回对象之前填充该对象,但是我没有成功。
Here's the code that is working so far (but only returning the alert
object's shell): 到目前为止,这是起作用的代码(但仅返回
alert
对象的外壳程序):
Parse.Cloud.define("alertLookup", function (request, response) {
Parse.Cloud.useMasterKey();
var codeQuery = new Parse.Query("code");
codeQuery.equalTo("value", request.params.code);
codeQuery.include("item");
codeQuery.find().then(function (codes) {
if (codes.length === 0) {
response.success("no item");
} else {
var code = codes[0];
var item = code.get("item");
var alert = item.get("alert");
if (alert === null || alert === undefined) {
response.success("no item");
} else {
response.success(alert);
}
}
}, function (error) {
response.error(error);
});
});
Here's what I have tried that is failing with an error code of 141: 这是我尝试失败的错误代码为141的内容:
Parse.Cloud.define("alertLookup", function (request, response) {
Parse.Cloud.useMasterKey();
var codeQuery = new Parse.Query("code");
codeQuery.equalTo("value", request.params.code);
codeQuery.include("item");
codeQuery.find().then(function (codes) {
if (codes.length === 0) {
response.success("no item");
} else {
var code = codes[0];
var item = code.get("item");
var alert = item.get("alert");
if (alert === null || alert === undefined) {
response.success("no item");
} else {
return alert.fetch();
}
}
}).then(function (a) {
response.success(a);
}, function (error) {
response.error(error);
});
});
Why won't the fetch()
call work properly? 为什么
fetch()
调用不能正常工作? When I insert console.log()
statements, although alert
is non-null, return alert.fetch();
当我插入
console.log()
语句时,尽管alert
不为null,但return alert.fetch();
does not ever seem to be called. 似乎从未被调用过。 At least, the
response.success(a);
至少,
response.success(a);
line is never called. 线永远不会被调用。 Why not?
为什么不?
Try this instead while chaining Promises: 在链接Promises时尝试以下方法:
codeQuery.find().then(function (codes) {
if (codes.length != 0) {
var code = codes[0];
var item = code.get("item");
var alert = item.get("alert");
if (alert != null && alert != undefined) {
var alertObj = new Parse.Object("alert"); // alert class ???
alertObj.id = alert.id;
return alertObj.fetch();
}
}
// return a Promise for no items
return Parse.Promise.as("no item");
}).then(function (a) {
response.success(a);
}, function (error) {
response.error(error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.