【问题标题】:TypeError: Cannot read property 'length' of null MongoDb errorTypeError:无法读取 null MongoDb 错误的属性“长度”
【发布时间】:2018-07-20 01:19:42
【问题描述】:

在查询中获取 mongodb 错误。这里的错误:

TypeError: 无法读取 null 的属性“长度”

代码如下:

// Open a connection with MongoDB

MongoClient.connect(process.env.MONGO_URL, (error, db) => {
  let dbo = db.db('gamblenomore');

//Running a query to find all customers that are in timezone CST.

dbo.collection('customers').find({therapyConfirmation: true, therapyTimeZone: 'CST', therapyDay: { $gt: 0 } }).toArray((error, result) => {
    let max = result.length;
    if (error) {
      throw error;
    }

'customers' 中目前没有符合上述条件的文档。

【问题讨论】:

  • 您应该在尝试使用result之前检查错误
  • 我一定要疯了,当我在 heroku 中重新部署任何想法时,错误不再存在,除了您的建议之外,我什么也没做:dbo.collection('customers').find({therapyConfirmation: true,therapyTimeZone: 'CST',therapyDay: { $gt: 0 } }).toArray((error, result) => { if (error) { throw error; } let max = result.length;

标签: javascript node.js mongodb npm


【解决方案1】:

将您的代码更改为:

dbo.collection('customers').find({
  therapyConfirmation: true,
  therapyTimeZone: 'CST', 
  therapyDay: { $gt: 0 } 
}).toArray((error, result) => {
    if (error) {
      throw error;
    }
    let max = result.length;
    // some other code
});

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2021-08-15
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多