目前,我有一个名为“约会”的表格--关于约会,我有一个客户关系。
在搜索解析文档时,我还没有找到关于如何在检索约会时急切地获取客户端的所有子集合的大量帮助。我尝试了一个标准查询,如下所示:
var Appointment = Parse.Object.extend("Appointment");
var query = new Parse.Query(Appointment);
query.equalTo("User",Parse.User.current());
query.include('Rate'); // a pointer object
query.find().then(function(appointments){
let appointmentItems =[];
for(var i=0; i < appointments.length;i++){
var appt = appointments[i];
var clientRelation = appt.relation('Client');
clientRelation.query().find().then(function(clients){
appointmentItems.push(
{
objectId: appt.id,
startDate : appt.get("Start"),
endDate: appt.get("End"),
clients: clients, //should be a Parse object collection
rate : appt.get("Rate"),
type: appt.get("Type"),
notes : appt.get("Notes"),
scheduledDate: appt.get("ScheduledDate"),
confirmed:appt.get("Confirmed"),
parseAppointment:appt
}
);//add to appointmentitems
}); //query.find
}
});这不会返回一个正确的客户端集合--然后我切换到尝试用云代码进行此操作--因为我假设问题在我这一边,无论出于什么原因,我认为我应该创建一个功能,只在他们的服务器上执行相同的操作,以减少网络调用的数量。这一职能的定义如下:
Parse.Cloud.define("GetAllAppointmentsWithClients",function(request,response){
var Appointment = Parse.Object.extend("Appointment");
var query = new Parse.Query(Appointment);
query.equalTo("User", request.user);
query.include('Rate');
query.find().then(function(appointments){
//for each appointment, get all client items
var apptItems = appointments.map(function(appointment){
var ClientRelation = appointment.get("Clients");
console.log(ClientRelation);
return {
objectId: appointment.id,
startDate : appointment.get("Start"),
endDate: appointment.get("End"),
clients: ClientRelation.query().find(),
rate : appointment.get("Rate"),
type: appointment.get("Type"),
notes : appointment.get("Notes"),
scheduledDate: appointment.get("ScheduledDate"),
confirmed:appointment.get("Confirmed"),
parseAppointment:appointment
};
});
console.log('apptItems Count is ' + apptItems.length);
response.success(apptItems);
})});
所返回的“客户端”与实际的对象类完全不一样:
clients: {_rejected: false, _rejectedCallbacks: [], _resolved: false, _resolvedCallbacks: []}当我浏览数据时,我可以很好地看到相关对象。实际上,Parse无法在同一调用中急切地获取关系查询,这似乎有些奇怪,但在这一点上,如果数据被正确检索,我将承担额外调用的开销。
任何帮助都是有益的,谢谢。
发布于 2016-02-24 14:22:02
最后我用"系列承诺“解决了这个问题
最后的代码如下所示:
var Appointment = Parse.Object.extend("Appointment");
var query = new Parse.Query(Appointment);
query.equalTo("User",Parse.User.current());
query.include('Rate');
var appointmentItems = [];
query.find().then(function(appointments){
var promise = Parse.Promise.as();
_.each(appointments,function(appointment){
promise = promise.then(function(){
var clientRelation = appointment.relation('Clients');
return clientRelation.query().find().then(function(clients){
appointmentItems.push(
{
//...object details
}
);
})
});
});
return promise;
}).then(function(result){
// return/use appointmentItems with the sub-collection of clients that were fetched within the subquery.
});很明显,您可以并行完成这一任务,但实际上并不需要这样做,因为我使用的查询似乎是即时返回的。我摆脱了云代码--因为它似乎没有提供任何性能提升。我要说的是,您不能调试云代码的事实似乎确实是有限的,我浪费了一些时间等待console.log语句在云代码面板的日志上显示自己--总的来说,Parse.Promise对象是使它正常工作的关键。
发布于 2016-02-23 14:43:55
在云代码示例中- ClientRelation.query().find()将返回一个Parse.Promise。因此,输出clients: {_rejected: false, _rejectedCallbacks: [], _resolved: false, _resolvedCallbacks: []}是有意义的--这就是控制台中的承诺。ClientRelation.query().find()将是一个异步调用,因此您的response.success(apptItems)将在您完成之前发生。
不过,在我看来,您的第一个例子看起来不错。如果您只像下面这样输出clients响应,您会看到它是什么?你确定你得到了一个Parse.Objects数组吗?你有空的[]吗?(这意味着,您正在查询的具有client关系的对象实际上是否添加了客户端?)
clientRelation.query().find().then(function(clients){
console.log(clients); // Check what you're actually getting here.
});还有一件很有帮助的事。在任何给定的约会对象中,您会有超过100个客户端吗?Parse.Relation实际上是用于非常大的其他对象的相关集合。如果您知道约会的相关对象不会超过100个(经验法则),那么一个更简单的方法是将客户端对象存储在Array中的Appointment对象中。
有了Parse.Relation,您就不能绕开第二个查询来获得相关的集合(客户机或云)。但是使用数据类型Array,您可以执行以下操作。
var query = new Parse.Query(Appointment);
query.equalTo("User", request.user);
query.include('Rate');
query.include('Clients'); // Assumes Client column is now an Array of Client Parse.Objects
query.find().then(function(appointments){
// You'll find Client Parse.Objects already nested and provided for you in the appointments.
console.log(appointments[0].get('Clients'));
});https://stackoverflow.com/questions/35574758
复制相似问题