首页 > 解决方案 > TypeError:无法读取 null MongoDb 错误的属性“长度”

问题描述

在查询中获取 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'符合上述标准的文件。

标签: javascriptnode.jsmongodbnpm

解决方案


将您的代码更改为:

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
});

推荐阅读