我正在尝试使用Parse Cloud Code并使用promises来确保我消除cals异步的任何问题。我想查询一个类,获取一个对象,构建一些JSON,然后查询对象的一些关系(标签,裁判)并将它们添加到JSON以返回ExpressJS进行渲染。我粘贴的代码不起作用,但我不明白为什么每次我返回项目以获得下一个查询的承诺。
修改
//Return a single project
Parse.Cloud.define('getProject', function(request, response) {
var projectUrl = request.params.projectUrl;
var project;
var projectsData = [];
var Projects = new Parse.Object("projects");
var query = new Parse.Query(Projects);
query.equalTo("projectUrl", projectUrl);
query.find().then(function(projectsResult) {
console.log(projectsResult.length + " Projects returned");
project = projectsResult[0];
var projectData = {
"id": project.get("id"),
"title": project.get("title"),
"previewImage": project.get("previewImage"),
"longDescription": project.get("longDescription"),
"shortDescription": project.get("shortDescription"),
"visibleToPublic": project.get("visibleToPublic"),
"dateStart": project.get("dateStart"),
"dateEnd": project.get("dateEnd"),
updatedAt: project.get("updatedAt"),
projectStatus: project.get("projectStatus")
};
projectsData.push(projectData);
console.log("Step 1. Projects Data: " + JSON.stringify(projectsData));
var tagsQuery = project.relation('tags');
return tagsQuery.query().find();
}).then(function(tags) {
var tagsData = [];
for(var t = 0; t < tags.length; t++) {
var tagData = {
"tag": tags[t].get("tag"),
}
console.log("Tag Data: " + tagData);
tagsData.push(tagData);
}
projectsData[tags] = tagsData;
console.log("Step 2. Tags Data: " + JSON.stringify(tagsData));
var refereesQuery = project.relation('referees');
return refereesQuery.query().find();
}).then(function(referees) {
var refereesData = [];
for(var r = 0; r < referees.length; r++) {
var refereeData = {
"name": referees[r].get("name"),
"role": referees[r].get("role"),
"emailAddress": referees[r].get("emailAddress"),
"phoneNumber": referees[r].get("phoneNumber"),
"linkedInUrl": referees[r].get("linkedInUrl"),
}
console.log("Referee Data: " + refereeData);
refereesData.push(refereeData);
}
projectsData[referees] = refereesData;
console.log("Step 3. Referees Data: " + JSON.stringify(refereesData));
console.log("Everthing should be part of Projects Data here: " + JSON.stringify(projectsData));
response.success(projectsData);
}, function(error) {
response.error("Error: " + error);
});
});
答案 0 :(得分:0)
问题似乎与您的初始查询有关。您正在尝试使用子类,但语法错误。
您的查询应该像这样开始:
var Projects = Parse.Object.extend("projects");
var query = new Parse.Query(Projects);
修改强>
另外,你正在返回对象,你应该返回promises。 请参阅下面的代码(未测试):
Parse.Cloud.define('getProject', function(request, response) {
var projectUrl = request.params.projectUrl;
var projectsData = [];
var Projects = Parse.Object.extend("projects");
var query = new Parse.Query(Projects);
query.equalTo("projectUrl", projectUrl);
query.find().then(function(projectsResult) {
console.log(projectsResult.length + " Projects returned");
var project = projectsResult[0];
var projectData = {
"id": project.get("id"),
"title": project.get("title"),
"previewImage": project.get("previewImage"),
"longDescription": project.get("longDescription"),
"shortDescription": project.get("shortDescription"),
"visibleToPublic": project.get("visibleToPublic"),
"dateStart": project.get("dateStart"),
"dateEnd": project.get("dateEnd"),
updatedAt: project.get("updatedAt"),
projectStatus: project.get("projectStatus")
};
projectsData.push(projectData);
console.log("Step 1. Projects Data: " + JSON.stringify(projectsData));
// Removed the return from here
var tagsData = [];
console.log("Project Step 2: " + JSON.stringify(project));
var tagsQuery = project.relation('tags');
return tagsQuery.query().find();
}).then(function(tags) {
for(var t = 0; t < tags.length; t++) {
var tagData = {
"tag": tags[t].get("tag"),
}
tagsData.push(tagData);
}
projectsData.tags = tagsData;
console.log("Step 2. Tags Data: " + JSON.stringify(tagsData));
// Removed return from here
var refereesData = [];
var refereesQuery = project.relation('referees');
return refereesQuery.find();
}).then(function(referees) {
for(var r = 0; r < referees.length; r++) {
var refereeData = {
"name": referees[r].get("name"),
"role": referees[r].get("role"),
"emailAddress": referees[r].get("emailAddress"),
"phoneNumber": referees[r].get("phoneNumber"),
"linkedInUrl": referees[r].get("linkedInUrl"),
}
refereesData.push(refereeData);
}
projectData.referees =refereesData;
console.log("Step 3. Referees Data: " + JSON.stringify(refereesData));
response.success(projectsData);
}, function(error) {
response.error("Error: " + error);
});
});
请参阅解析有关使用子类检索数据的文档here。
答案 1 :(得分:0)
有几个问题:
projectsData
和projectData
projectsData
实际上并不是必需的。projectData
需要在创建它之后(或应该)使用的三个位置范围内。分配.then()
用于标准同步流程就足够了。清除不必要的.thens将有助于彻底解决范围问题。除了改变代码之外,我还做了以下几点:
Parse.Cloud.define('getProject', function(request, response) {
var Projects = Parse.Object.extend("projects"); // with credit to @kRiZ
var query = new Parse.Query(Projects);
query.equalTo('projectUrl', request.params.projectUrl);
query.find().then(function(projectsResult) {
var project = projectsResult[0];
var projectData = {
'id': project.get('id'),
'title': project.get('title'),
'previewImage': project.get('previewImage'),
'longDescription': project.get('longDescription'),
'shortDescription': project.get('shortDescription'),
'visibleToPublic': project.get('visibleToPublic'),
'dateStart': project.get('dateStart'),
'dateEnd': project.get('dateEnd'),
'updatedAt': project.get('updatedAt'),
'projectStatus': project.get('projectStatus')
};
//Now make the tags query and the referees query in parallel.
var tagsPromise = project.relation('tags').query().find();
var refereesPromise = project.relation('referees').query().find();
// Aggregate the two promises with Parse.Promise.when(), and handle the responses.
return Parse.Promise.when(tagsPromise, refereesPromise).then(function(tags, referees) {
//Process the tags response
projectData.tags = tags.map(function(t) {
return {
'tag': t.get('tag')
};
});
//Process the referees response
projectData.referees = referees.map(function(r) {
return {
'name': r.get('name'),
'role': r.get('role'),
'emailAddress': r.get('emailAddress'),
'phoneNumber': r.get('phoneNumber'),
'linkedInUrl': r.get('linkedInUrl')
};
});
// Yay!
response.success(projectData);
});
}).fail(function(error) {
response.error('Error: ' + error);
});
});
除了整体重组外,唯一重大的变化是:
Array#map()
将数组映射到另一个数组。Parse.Promise.when()
汇总两个承诺。