我一直在使用node-readability
处理某些事情,而且我对导致此错误的原因感到困惑。
var url = req.body.url;
// Now have body, let's unfluff
read(url, function(err, data, meta) {
console.log('title:' + data.title);
console.log('typeof: ' + typeof(data.title));
// Create a new article using data from readability
var article = new Article({
title: data.title,
raw: data.content,
image: data.document.getElementsByTagName('meta')['og:image'].getAttribute('content'), // Could be improved
source: url // Should strip to just domain?
});
console.log(article);
// Close article to clean up jsdom and prevent leaks
//data.close();
// Save article
article.save(function(err) {
if (err) return next(err);
res.redirect('/');
});
});
使用测试网址运行时,会将以下内容记录到控制台。
title:Gardening: Time to grow potatoes, ask the expert, and jobs for the week
typeof: string
[redacted]/controllers/temp.js:56
console.log('title:' + data.title);
^
TypeError: Cannot read property 'title' of undefined
at [redacted]/controllers/temp.js:56:32
at Object.jsdom.env.done ([redacted]/node_modules/node-readability/src/readability.js:234:18)
at [redacted]/node_modules/node-readability/node_modules/jsdom/lib/jsdom.js:270:18
at process._tickCallback (node.js:442:13)
所以,显然它可以读取标题,因为它打印出来。它立即导致TypeError出于某种未知原因。 typeof
返回字符串,但是在返回任何其他内容之前记录整个数据对象:
[...]
content: [Getter],
title: [Getter],
html: [Getter],
document: [Getter],
[...]
很明显,它不是标准字符串。然而,根据文档,它不使用任何类型的延迟处理(这样就可以运行data.title()
来处理标题,减少大数据集的负载)。试图这样做会导致类似的错误:
TypeError: Cannot call method 'title' of undefined
at [redacted]/controllers/temp.js:56:22
at Request._callback ([redacted]/node_modules/node-readability/src/readability.js:172:16)
at self.callback ([redacted]/node_modules/node-readability/node_modules/request/request.js:123:22)
答案 0 :(得分:0)
在回调的某个地方console.log('typeof: ' + typeof(data.title));
和console.log(article);
之间,会抛出错误。将回调中的部分内容包裹在try/ catch
内,并在catch
中记录错误,看看它是什么。
要了解为什么发生这种情况,请查看the readability source code;
try {
var readability = new Readability(window, options);
// add meta information to callback
callback(null, readability, meta);
} catch (ex) {
window.close();
return callback(ex);
}
在您的情况下,可读性已成功完成,is invoking your callback, passing null, readability, meta
as the parameters。但是,您的回调会导致异常,可读性源代码中的catch
会捕获该异常。
您的回调是called again by readability,但这次仅通过ex
。 此时您的回调尝试调用data.title,
,但data
未定义,导致另一个异常,其中不是} 在try/ catch
内,导致异常终止您的程序。
这是可读性开发人员的疏忽;他们不应该在try/catch
内拨打你的回叫。它应该看起来像这样;
var readability;
try {
readability = new Readability(window, options);
} catch (ex) {
window.close();
return callback(ex);
}
callback(null, readability, meta);