【发布时间】:2019-01-17 21:45:33
【问题描述】:
我在我的 mlab 数据库中存储了 2775 个 URL,然后我将每个 URL 删除以获取更多信息。我存储在一个数组中的所有 URL 然后将其传递给一个函数来处理。但是,代码最多只能运行大约 1700 个 url 并处理它然后停止。这是我的代码(对不起,这是我第一次使用stackoverflow:
Product.find({}, (err, foundProducts) => {
if (err) {
console.log("err " + err);
} else {
foundProducts.forEach(function(foundProduct) {
var updateProduct = service.updateTikiProduct(foundProduct.url);
});
}
});
updateTikiProduct: function(url) {
const options = {
url: url,
json: true
};
request(options,
function(err, res, body) {
// SOME code to crawl data
Product.findOneAndUpdate({
url: options.url
}, {
$set: {
name: name,
brand: brand,
store: store,
location: location,
base_category: categoryType,
top_description: topDescription,
feature_description: featureDescription
}
}, {
upsert: true,
new: true
}, (err, createdProduct) => {
if (err) {
reject(err);
} else {
var currentDate = new Date();
if (!createdProduct.hasOwnProperty("price")) {
createdProduct.price.push({
current: currentPrice,
origin: originPrice
});
createdProduct.save();
} else if (createdProduct.hasOwnProperty("price") &&
createdProduct.price[0].date.getDate() != currentDate.getDate()) {
createdProduct.price.push({
current: currentPrice,
origin: originPrice
});
createdProduct.save();
console.log("Update price");
}
counter++;
console.log("url : " + options.url);
console.log("Created product " + counter + " success!");
}
});
}
【问题讨论】:
-
代码停止时控制台是否有错误?
-
不行,代码运行的时候,我的内存快满了(大约90%~95%)。但是当它达到大约 1700 url 时,内存正常返回并且控制台停止运行。但是,控制台并没有告诉我它停止工作
标签: javascript node.js