Javascript 查询解析表并急切地获取关系以进行匹配
目前,我有一个名为“约会”的表-在约会中,我有一个客户关系 在搜索解析文档的过程中,我没有找到大量关于如何在检索约会时急切地获取所有客户端子集合的帮助。我尝试了一个标准查询,如下所示:Javascript 查询解析表并急切地获取关系以进行匹配,javascript,parse-platform,Javascript,Parse Platform,目前,我有一个名为“约会”的表-在约会中,我有一个客户关系 在搜索解析文档的过程中,我没有找到大量关于如何在检索约会时急切地获取所有客户端子集合的帮助。我尝试了一个标准查询,如下所示: var Appointment = Parse.Object.extend("Appointment"); var query = new Parse.Query(Appointment); query.equalTo("User",Parse.User.current()); query.include
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
}
});
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.
});
}))
结果返回的“客户机”与实际的对象类完全不同:
clients: {_rejected: false, _rejectedCallbacks: [], _resolved: false, _resolvedCallbacks: []}
当我浏览数据时,我可以很好地看到相关的对象。Parse不能在同一调用中急切地获取关系查询,这一事实似乎有点奇怪,因为来自其他数据提供者,但在这一点上,如果数据被正确检索,我将承担额外调用的开销
任何帮助都是有益的,谢谢。在您的云代码示例中-
ClientRelation.query().find()
将返回Parse.Promise。因此,输出<代码>客户端:{u拒绝:false,{u拒绝回调:[],{u解析:false,{u解析回调:[]}是有意义的-这就是控制台中的承诺。ClientRelation.query().find()
将是一个异步调用,因此您的响应.success(apptItems)
将在完成之前发生
但就我所知,你的第一个例子看起来不错。如果您只是像下面这样输出,客户机的响应是什么?你确定得到的是Parse.Objects数组吗?您得到的是空的[]
?(也就是说,您正在查询的具有client
关系的对象是否确实添加了客户端?)
还有一件事很有帮助。在任何给定的约会对象中,您是否会有100多个客户端?关系实际上是指其他对象的非常大的相关集合。如果您知道您的约会将不会有超过100个(经验法则)的相关对象-一种更简单的方法是将您的客户端对象存储在约会对象中的数组中
对于Parse.Relation,您无法回避必须进行第二次查询以获取相关集合(客户端或云)。但是使用数据类型数组
可以执行以下操作
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'));
});
我最终用“”解决了这个问题
最终的代码如下所示:
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
}
});
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语句在云代码面板的日志上显示出来——总体而言,解析。Promise对象是使其正常工作的关键。如果对象被更新,使用解析对象数组不会失败,但是数组没有更新以匹配新的对象字段?我在考虑为给定的客户搜索约会,如果客户记录被修改,那么我无法执行包含操作,因为数组中的对象将代表客户的早期版本,不是吗?此外,我尝试在then函数中使用.then()和逻辑将新约会记录应用于数组,但“then”函数从未启动。我还尝试在查询中使用success函数,但它也没有触发,我不太确定我是否理解您在第一个问题中的意思。但我的意思是基本上重做该列,并将数据类型设置为Array而不是Relation。如果这是一个选项,那么如果要在一个调用中调用嵌套数据,那就需要考虑一下。如果你已经有数据的话,可能不值得为过渡做一些云计算的工作,因为看起来你已经得到了你需要做的事情。