首页 > 解决方案 > 错误:配置属性“db”未定义'Node.Js

问题描述

运行以下代码时出现错误。

包.json

{
  "name": "movies-api-model",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --watchAll --verbose"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "config": "^1.29.4",
    "express": "^4.17.1",
    "express-async-errors": "^2.1.0",
    "fawn": "^2.1.5",
    "joi": "^13.1.0",
    "joi-objectid": "^2.0.0",
    "joi-oid": "^1.1.2",
    "jsonwebtoken": "^8.1.1",
    "lodash": "^4.17.4",
    "mongoose": "^5.10.10",
    "winston": "^2.4.0",
    "winston-mongodb": "^3.0.0"
  },
  "devDependencies": {
    "jest": "^22.2.2"
  }
}

配置文件夹

测试.json

{
    "jwtPrivateKey": "1234",
    "db": "mongodb://localhost/vidly_tests"
}

默认.json

{
    "jwtPrivateKey": "",
    "db": "mongodb://localhost/vidly"
}

数据库.js

const winston = require('winston');
const mongoose = require('mongoose');
const config = require('config');

module.exports = function() {    
    const db = config.get('db');
    mongoose.connect(db)//,{ useNewUrlParser: true,  useUnifiedTopology: true, useCreateIndex: true}
    .then(() => winston.info(`Connected to ${db}`));  
}

错误_

'错误:未定义配置属性“db”','在 Config.get (C:\Users\projects\movies-api-model\node_modules\config\lib\config.js:194:11)','在module.exports (C:\Users\projects\movies-api-model\startup\db.js:6:23)', ' 在 Object. (C:\Users\projects\movies-api-model\index.js:7:24)', ' 在 Module._compile (internal/modules/cjs/loader.js:1138:30)', ' 在 Object. Module._extensions..js (internal/modules/cjs/loader.js:1158:10)','在 Module.load (internal/modules/cjs/loader.js:986:32)','在 Function.Module ._load (internal/modules/cjs/loader.js:879:14)', ' 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)', ' 在 internal/main/run_main_module .js:17:47'

任何帮助,将不胜感激

标签: javascriptnode.jsmongodbintegration-testing

解决方案


推荐阅读