Javascript 未将本地阵列推送到
我有以下代码。尽管成功检索了Javascript 未将本地阵列推送到,javascript,arrays,Javascript,Arrays,我有以下代码。尽管成功检索了ustock.unitprice,但数组价格似乎没有被推送到prices数组 getLatestMarketPrices: function(username, callback) { var prices = []; db.find('portfolio', {user: username}, function(err, stocks) { for(var i = 0; i < stocks.length; i++) { m
ustock.unitprice
,但数组价格似乎没有被推送到prices
数组
getLatestMarketPrices: function(username, callback) {
var prices = [];
db.find('portfolio', {user: username}, function(err, stocks) {
for(var i = 0; i < stocks.length; i++) {
module.exports.getQuote(stocks[i].stock, function(err, ustock) {
console.log(ustock.unitprice); // Retrieves 1.092
prices.push(ustock.unitprice); // Should push to prices array?
});
}
console.log(prices); // Prices is still [] despite earlier push.
callback(null, prices);
});
},
getLatestMarketPrices:函数(用户名,回调){
var价格=[];
db.find('portfolio',{user:username},函数(err,stocks){
对于(var i=0;i
这是一个范围问题吗?我不太清楚为什么价格没有被推到
非常感谢。如果您了解jquery,可以尝试使用延迟对象
getLatestMarketPrices: function(username, callback) {
var prices = [];
var defer = $.Deferred();
//Attach a handler to be called when the deferred object is resolved
defer.done(function(){
console.log(prices);
callback(null, prices);
});
db.find('portfolio', {user: username}, function(err, stocks) {
for(var i = 0; i < stocks.length; i++) {
module.exports.getQuote(stocks[i].stock, function(err, ustock) {
console.log(ustock.unitprice); // Retrieves 1.092
prices.push(ustock.unitprice); // Should push to prices array?
//resolve when we retrieve all
if (prices.length == stocks.length){
defer.resolve();
}
});
}
});
},
getLatestMarketPrices:函数(用户名,回调){
var价格=[];
var defer=$.Deferred();
//附加在解析延迟对象时要调用的处理程序
defer.done(函数(){
控制台.日志(价格);
回调(空,价格);
});
db.find('portfolio',{user:username},函数(err,stocks){
对于(var i=0;i
更新:或根本不需要延迟对象:
getLatestMarketPrices: function(username, callback) {
var prices = [];
db.find('portfolio', {user: username}, function(err, stocks) {
for(var i = 0; i < stocks.length; i++) {
module.exports.getQuote(stocks[i].stock, function(err, ustock) {
console.log(ustock.unitprice); // Retrieves 1.092
prices.push(ustock.unitprice); // Should push to prices array?
//callback only when we receive all
if (prices.length == stocks.length){
console.log(prices);
callback(null, prices);
}
});
}
});
},
getLatestMarketPrices:函数(用户名,回调){
var价格=[];
db.find('portfolio',{user:username},函数(err,stocks){
对于(var i=0;i
如果您知道jquery,可以尝试使用延迟对象
getLatestMarketPrices: function(username, callback) {
var prices = [];
var defer = $.Deferred();
//Attach a handler to be called when the deferred object is resolved
defer.done(function(){
console.log(prices);
callback(null, prices);
});
db.find('portfolio', {user: username}, function(err, stocks) {
for(var i = 0; i < stocks.length; i++) {
module.exports.getQuote(stocks[i].stock, function(err, ustock) {
console.log(ustock.unitprice); // Retrieves 1.092
prices.push(ustock.unitprice); // Should push to prices array?
//resolve when we retrieve all
if (prices.length == stocks.length){
defer.resolve();
}
});
}
});
},
getLatestMarketPrices:函数(用户名,回调){
var价格=[];
var defer=$.Deferred();
//附加在解析延迟对象时要调用的处理程序
defer.done(函数(){
控制台.日志(价格);
回调(空,价格);
});
db.find('portfolio',{user:username},函数(err,stocks){
对于(var i=0;i
更新:或根本不需要延迟对象:
getLatestMarketPrices: function(username, callback) {
var prices = [];
db.find('portfolio', {user: username}, function(err, stocks) {
for(var i = 0; i < stocks.length; i++) {
module.exports.getQuote(stocks[i].stock, function(err, ustock) {
console.log(ustock.unitprice); // Retrieves 1.092
prices.push(ustock.unitprice); // Should push to prices array?
//callback only when we receive all
if (prices.length == stocks.length){
console.log(prices);
callback(null, prices);
}
});
}
});
},
getLatestMarketPrices:函数(用户名,回调){
var价格=[];
db.find('portfolio',{user:username},函数(err,stocks){
对于(var i=0;i
是db.find异步调用吗?是。它有一个回调(函数(err,stocks)
)。你认为我必须在每次迭代时回调到外部数组吗?谢谢。这是因为在getQuote()
完成之前调用了console.log
。@Andy但是在上次回调中返回了[]数组。我正在通过客户端的WebSocket进行侦听,返回的数组仍然为空。正如@Andy所说,在异步方法完成任务并将所有值推入数组之前,您的console.log(prices)调用正在运行。是否为db.find a async调用?是否。它有一个回调(函数(err,stocks)
)。你认为我必须在每次迭代时回调到外部数组吗?谢谢。这是因为在getQuote()
完成之前调用了console.log
。@Andy但是在上次回调中返回了[]数组。我正在通过客户端的WebSocket进行监听,返回的数组仍然为空。正如@Andy所说,在异步方法完成it任务并将所有值推入数组之前,您的console.log(prices)调用正在运行。谢谢,Khanh。“我会试试这个,让你知道的。”萨姆·圣佩特森:我做了一些调整,谢谢。在您的更新之前尝试了“延迟”,但效果良好。:)在没有我能确认的情况下工作。不过,了解延迟是很有趣的;因此,即使我减少了代码行,我也可能以另一种方式保留在注释中。再次感谢谢谢,康。“我会试试这个,让你知道的。”萨姆·圣佩特森:我做了一些调整,谢谢。在您的更新之前尝试了“延迟”,但效果良好。:)在没有我能确认的情况下工作。不过,了解延迟是很有趣的;因此,即使我减少了代码行,我也可能以另一种方式保留在注释中。再次感谢