Javascript 将Parse.Query检索的对象分配给变量
我正在尝试做一些看起来应该非常简单的事情,但是已经连续搜索了好几个小时,却没有任何运气。我只想为给定的objectId查询Parse.Object的子类(理想情况下使用.get()但是.find或.first也可以),并将整个对象分配给一个我可以在查询之外访问的变量。我知道查询会返回承诺,我可以从.then()函数内部记录要捕获的对象,但我不知道如何在其他地方访问它。这是我现在所拥有的,我认为我在正确的轨道上Javascript 将Parse.Query检索的对象分配给变量,javascript,backbone.js,parse-platform,Javascript,Backbone.js,Parse Platform,我正在尝试做一些看起来应该非常简单的事情,但是已经连续搜索了好几个小时,却没有任何运气。我只想为给定的objectId查询Parse.Object的子类(理想情况下使用.get()但是.find或.first也可以),并将整个对象分配给一个我可以在查询之外访问的变量。我知道查询会返回承诺,我可以从.then()函数内部记录要捕获的对象,但我不知道如何在其他地方访问它。这是我现在所拥有的,我认为我在正确的轨道上 var thisLunchQuery = new Parse.Query(
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
console.log(results[0]);
});
这将记录我想要访问的整个对象,并且我可以在控制台中看到它的属性。但是,我希望能够在代码的其他地方访问“thisLunch”变量。就目前而言,我不能那样做。我假设这是因为变量的范围。我如何定义该变量,使其具有更大的范围
下面是一个更全面的代码和平,展示了我正在尝试做的事情
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
//not sure what goes here
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: thisLunch
};
$(this.el).html(this.template(templateArgs));
return this;
this.delegateEvents();
}
如您所见,我正在尝试获取对象,以便将其传递到模板并访问其属性。我希望这会有帮助,我对这一切都很陌生
更新:
下面是我尝试链接查询的最佳尝试,这样我可以使用第一个查询的部分结果作为第二个查询的参数,然后在最终回调函数中使用两个查询的对象
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var thisLocationQuery = new Parse.Query(Location);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
lunchObject = results[0];
thisLocationQuery.equalTo("objectId", results[0].get("locationId");
thisLocationQuery.find.then(assignLocation);
}
function assignLocation(locationResults, lunchObject){
// I'd like to be able to access the location I just found (locationResults[0]) as
//well as the original lunchId from the first query inside this callback
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: lunchObject,
thisLocation: locationResults[0];
};
$(this.el).html(this.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
return this;
this.delegateEvents();
}
很明显,这并不是安静的工作,但我不确定如何像我正在尝试的那样传递数据。也许我应该使用。什么时候?您可以使用thisLunch作为参数调用函数,如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
funcName(results[0])
});
或者将其放在全局/窗口对象中,如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
window.myLunch = results[0];
});
您需要做的是定义一个函数,该函数对结果执行您想要执行的操作,并将其传递给promise success方法。可能是这样的:
function doSomething(result) {
// do your stuff here
}
thisLunchQuery.find().then(doSomething)
更新:
在视图的上下文中,代码可能类似于:
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
var self = this;
function assignLunch(results){
var templateArgs = {
thisPlans: this.model.toJSON(),
thisLunch: results
};
self.$el.html(self.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunchQuery.find().then(assignLunch);
return this;
}
我似乎很难让这两个选项中的任何一个发挥作用。它们中的任何一个是否要求我删除thiisLunchQuery.find()前面的“var thisLunch=”命令?另外,funcName中的代码是什么样子的?myLunch指的是什么?如何使用它将结果对象指定给变量?当我在doSomething函数中使用console.log(result[0])时,它会打印正确的对象,但我仍然认为这不会给出所需的结果。当我在doSomething中返回结果[0],然后执行thisLunch=thislunkQuery.find()。然后(doSomething),thisLunch仍然保留承诺,而不是对象。这是异步编程
thisLunch
不会有函数执行时返回的值,因为该值是异步计算的,在变量赋值时不可用。这就是我们在回调函数中处理结果的原因。这是有道理的。我对异步还不熟悉,很难理解它。我认为。然后的重点是等待查询完成,然后对结果进行处理。正如我所说,结果[0]在回调函数中保存了我试图捕获的对象。是否有可能使该对象在该函数之外可访问?我最终希望将它传递给一个新的模板声明。您可以轻松地将它分配给函数范围之外的变量。然后,您的问题将是知道何时将该值分配给该变量,因此,您可以通过回调继续前进。在这一点上,尝试和处理函数范围之外的变量就不再有意义了,你最好用一种更实用的方式来处理事情。如果你发布了一个具体的例子,说明你正在努力实现的目标,我可以给你一个具体的例子,说明你如何以“功能性”的方式来实现这个目标。太棒了,这正是我最初想要做的。我想我又遇到了一层复杂的问题,我被难住了。如何运行另一个查询,将第一个查询的结果用作参数,然后允许我在新回调函数中访问新结果和旧结果。我将把它标记为一个答案,因为它肯定回答了最初的问题,但如果您能够提供帮助,我将添加描述这个新问题的代码。