Javascript Node.js脚本停止,没有任何错误
我使用Javascript Node.js脚本停止,没有任何错误,javascript,node.js,Javascript,Node.js,我使用scraper模块和async模块的queue函数编写了一个scraper 我从一个json文件中读取要删除的URL列表,并将信息写入另一个json文件中 这是我的剧本: var fs = require("fs"); var scrap = require("scrap"), async = require("async"); var errors = []; // Queue a list of URLs var queue = JSON.parse(fs.readFil
scraper
模块和async
模块的queue
函数编写了一个scraper
我从一个json文件中读取要删除的URL列表,并将信息写入另一个json文件中
这是我的剧本:
var fs = require("fs");
var scrap = require("scrap"),
async = require("async");
var errors = [];
// Queue a list of URLs
var queue = JSON.parse(fs.readFileSync("products.json", "utf8"));
var len = queue.products.length;
var q = async.queue(function (url, done) {
scrap(url, function(err, $) {
var product = {};
product.name = $("#page-body h2").first().text().trim();
product.myarr = [];
product.picture = $(".content img").first().attr("src");
try {
if (product.picture.indexOf("someword") > 1) {
delete product.picture;
}
}
catch (e) {
console.error(e);
}
$(".content [style^=\"color: #\"] [style=\"font-weight: bold\"], .content [style=\"font-weight: bold\"] [style^=\"color: #\"]").each(function() {
product.myarr.push($(this).text().trim().toLowerCase());
});
if (product.myarr.length) {
fs.appendFile("products-parsed.json", JSON.stringify(product) + ",\n", function (err) {
console.log(queue.products.indexOf(url), len, err);
if (err) { errors.push(queue.products.indexOf(url)); }
done();
});
}
});
}, 20);
q.drain = function() {
console.log(errors);
};
q.push(queue.products);
当我运行它时,在大约3000页之后,它停止(退出)并且没有给出任何错误,我必须使用以下命令从最新的工作页开始:
q.push(queue.products.slice(lastWorkedPage, queue.products.length - 1));
如何解决此问题?不确定原因,顺便说一下,问题似乎是由这一行引起的:
console.log(queue.products.indexOf(url), len, err);
评论解决了问题,请随意给出更准确的答案,解释解决方案,我会将其设置为已接受。无法评论,因此我必须发布新答案 我可以确认console.log-错误。NodeJS/Express有时在尝试使用console.log()时会停止 来自一个测试项目的代码:
console.log(req.body.credentials.password, isMatch);
if (isMatch) {
sess.currentUser = user;
console.log(user);
res.send({ status: "ok", loginUser: user });
}
else {
res.send({ status : "error", msg: "Login failed!" });
}
第二条日志记录行(console.log(user))无误地停止NodeJS!这只发生在某些环境中-在大多数开发环境中,这一切都很好 哥们,你是如何在codepen中运行节点的?我刚刚用codepen共享了代码..然后最好粘贴在这里,codepen似乎会提供一个演示。我认为太长了,无法在那里发布。可能是你有溢出错误。如果产品相当大,那么将所有这些数据存储在
q
中可能会导致RAM过载。