Javascript 登录到控制台可以工作,但会立即导致TypeError

Javascript 登录到控制台可以工作,但会立即导致TypeError,javascript,Javascript,我一直在使用节点可读性进行一些工作,我对导致此错误的原因感到困惑 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

我一直在使用节点可读性进行一些工作,我对导致此错误的原因感到困惑

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('/');
  });
});
当使用测试url运行时,以下内容将记录到控制台

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)
所以,很明显,它可以阅读标题,因为它会打印出来。由于某种未知的原因,它会立即导致类型错误。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)
TL;博士 在回调中的某个地方,在console.log'typeof:'+typeofdata.title之间;和控制台。日志物品;,正在抛出一个错误。将回调的这一部分包装在try/catch中,并将错误记录在catch中以查看它是什么

非TL;博士 要理解为什么会发生这种情况,请看一看

在您的情况下,可读性正在成功完成,并且。但是,回调会导致异常,可读性源代码中的捕获会捕获该异常

您的回拨正在进行,但这次只传递ex。这次回调尝试调用data.title,但数据未定义,导致另一个异常,该异常不在try/catch中,导致异常终止程序

这是可读性开发人员的疏忽;他们不应该在try/catch中调用您的回调。它应该看起来像这样

var readability;

try {
  readability = new Readability(window, options);
} catch (ex) {
  window.close();
  return callback(ex);
}

callback(null, readability, meta);

要完全清楚,temp.js的第56行,即抛出错误的地方是第二个console.log调用,例如,传递给read函数的回调的第二行,是吗?不是,第56行是回调的第一个console.log行。在console.log'title:'+data.title;,上触发typeerror;,它实际上只是打印了结果。事实上,它把它们打印出来,然后继续打印下一行,然后打印错误d。标题是一个getter,而不是一个简单的属性;您可以转到[redact]/node\u modules/node readability/src/readability.js并检查第234行是否匹配吗?是的,非常匹配。
var readability;

try {
  readability = new Readability(window, options);
} catch (ex) {
  window.close();
  return callback(ex);
}

callback(null, readability, meta);