Javascript 向数组中的对象追加新元素
我已经对这个循环困惑了几天,不能完全达到我所需要的 我循环搜索结果数组mdb_results,并从每个对象中提取一个.name作为Google CSE图像搜索中的_搜索词 Th CSE返回另一个数组CSE,我想将其附加到从中提取_搜索词mdb_results[I]的对象 我正在努力实现这样的目标Javascript 向数组中的对象追加新元素,javascript,node.js,loops,asynchronous,google-custom-search,Javascript,Node.js,Loops,Asynchronous,Google Custom Search,我已经对这个循环困惑了几天,不能完全达到我所需要的 我循环搜索结果数组mdb_results,并从每个对象中提取一个.name作为Google CSE图像搜索中的_搜索词 Th CSE返回另一个数组CSE,我想将其附加到从中提取_搜索词mdb_results[I]的对象 我正在努力实现这样的目标 [{"name":"FOO", "images":[{"img1":"http://thumbnail1"},{"img2":"http://thumbnail2"}] }, {"name":"F
[{"name":"FOO",
"images":[{"img1":"http://thumbnail1"},{"img2":"http://thumbnail2"}]
},
{"name":"FOOOO",
"images":[{"img1":"http://thumbnaila"},{"img2":"http://thumbnailb"}]
}]
有人能告诉我如何做到这一点吗
谢谢问题在于您希望异步操作同步工作:
router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
for (var i=0; i<mdb_results.length; i++){
var _search = mdb_results[i].name ;
// This search is asynchronous, it won't have returned by the time
// you return the result below.
ImageSearch(_search, function(cse){
// How to add cse.img to mdb_results[i].images ??
// mdb_results[i].images = cse;
// gives undefined
},0,2);
};
// At this point, ImageSearch has been called, but has not returned results.
res.send(mdb_results);
});
});
});
这是有效的,我接受了你的答案-谢谢。你介意详细说明一下cb的情况吗?尽管有效,我还是很难理解到底发生了什么。我更新了这篇文章,试图详细说明。理解回调如何在NodeJS中工作对于理解这里发生的事情至关重要。回调用于指示您已完成工作。感谢您的注释。我仍在努力处理返回回调,这似乎是对自身的调用?我们将回调传递到封闭函数中,然后在没有任何回调函数定义的情况下调用它。如果答案太复杂,无法发表评论,你能推荐一个我可以自学的资源吗?您提供了一个将结果和回调作为参数的函数。回调函数是异步库传入的函数。Async等待您调用该函数以确认您完成了异步工作。在每次迭代中调用回调后,Async就知道您完成了。如果您需要更多信息,请查找更多关于Nodejs回调的文章。这很难理解,但对Nodejs编程至关重要。
[{"name":"FOO",
"images":[{"img1":"http://thumbnail1"},{"img2":"http://thumbnail2"}]
},
{"name":"FOOOO",
"images":[{"img1":"http://thumbnaila"},{"img2":"http://thumbnailb"}]
}]
router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
for (var i=0; i<mdb_results.length; i++){
var _search = mdb_results[i].name ;
// This search is asynchronous, it won't have returned by the time
// you return the result below.
ImageSearch(_search, function(cse){
// How to add cse.img to mdb_results[i].images ??
// mdb_results[i].images = cse;
// gives undefined
},0,2);
};
// At this point, ImageSearch has been called, but has not returned results.
res.send(mdb_results);
});
});
});
router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
// async.forEach will iterate through an array and perform an asynchronous action.
// It waits for you to call callback() to indicate that you are done
// rather than waiting for it to execute synchronously.
async.forEach(mdb_results, function (result, callback) {
ImageSearch(result.name, function(cse){
// This code doesn't get executed until the network call returns results.
result.images = cse;
// This indicate that you are done with this iteration.
return callback();
},0,2);
},
// This gets call once callback() is called for each element in the array,
// so this only gets fired after ImageSearch returns you results.
function (err) {
res.send(mdb_results);
});
});
});