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

时间:2019-10-17 17:32:22

标签: javascript node.js mongodb loopbackjs mongodb-atlas

我尝试将我的Loopback应用程序与MongoDb Atlas连接,但出现错误“ TypeError:无法读取null的属性'dbName'”。

以下是我尝试过的内容:

  1. 运行lb数据源
  2. 填写所有必填信息
  3. 生成了这个javascript对象:
{
  "db": {
    "name": "db",
    "connector": "mongodb"
  },
  "mongodb": {
    "host": "",
    "port": 0,
    "url": "mongodb+srv://USER:PASSWORD@DATABASENAME-f4exx.mongodb.net/test?retryWrites=true&w=majority",
    "database": "DATABASENAME",
    "password": "PASSWORD",
    "name": "mongodb",
    "user": "dbAdmin",
    "useNewUrlParser": true,
    "connector": "mongodb"
  }
}
  1. 每次尝试运行“ node”。 ,得到TypeError: Cannot read property 'dbName' of null

  2. 我已将我的IP添加到列入白名单的IP(MongoDB地图集)中

  3. 下面是我的model.config.json

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "mongodb"
  },
  "AccessToken": {
    "dataSource": "mongodb",
    "public": false
  },
  "ACL": {
    "dataSource": "mongodb",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "mongodb",
    "public": false,
    "options": {
      "strictObjectIDCoercion": true
    }
  },
  "Role": {
    "dataSource": "mongodb",
    "public": false
  }
}

PS:我正在使用环回3x

1 个答案:

答案 0 :(得分:0)

发现将属性$dsn = getenv('MYSQL_DSN'); $user = getenv('MYSQL_USER'); $pw = getenv('MYSQL_PASSWORD'); //Database connection $db = new PDO($dsn, $user, $pw); //register value from <form> inputs $username = $_POST['username']; $password = $_POST['password']; //insertion success $statement = $db->prepare("insert into member values(null, '$username', SHA('$password'), now())"); $statement->execute(); 设置为false可以解决我的问题。仍然不知道为什么这是根本原因。