【发布时间】:2021-03-29 00:07:21
【问题描述】:
有谁知道这个错误是什么意思? 我在 Node.js 上有我的应用程序,在 Atlas 上有我的数据库。当我尝试在 Heroku 上部署它时,我在日志中收到以下错误消息:
TypeError:无法读取 null 的属性“拆分” parseSrvConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:40:23)在 解析连接字符串 (/app/node_modules/mongodb/lib/core/uri_parser.js:556:12)在 连接 (/app/node_modules/mongodb/lib/operations/connect.js:272:3)
在 /app/node_modules/mongodb/lib/mongo_client.js:218:5 在 也许承诺(/app/node_modules/mongodb/lib/utils.js:719:3)在 MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:214:10) 在 函数.MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:432:22) 在 对象处的 Database.connect (/app/database.js:9:15)。 (/app/database.js:21:4) 在 Module._compile (internal/modules/cjs/loader.js:1118:30) [31m[nodemon] 应用程序崩溃 - 在开始之前等待文件更改...[39m
数据库.js
const mongo = require('mongodb').MongoClient; require('dotenv').config()
class Database {
constructor() {
this.client = null;
}
connect() {
mongo.connect(process.env.URL_DATABASE, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) throw err
this.client = client;
});
} }
const db = new Database(); db.connect(); console.log(db); module.exports = db;
路由器.js
const express = require('express'),
router = express.Router();
const db = require('./database');
const ObjectId = require('mongodb').ObjectID;
// GET ALL
router.get('/books', (req, res, next) => {
const localdb = db.client.db(process.env.DB_NAME);
const collection = localdb.collection(process.env.COLL_BOOKS);
collection.find({}).toArray(function (err, docs) {
if (err) throw err
res.status(200).send(docs);
});
});
// GET ONE
router.get('/books/:name', (req, res, next) => {
let name = req.params.name
const localdb = db.client.db(process.env.DB_NAME);
const collection = localdb.collection(process.env.COLL_BOOKS);
collection.findOne({ "name": name }, function (err, docs) {
if (err) throw err
res.status(200).send(docs);
});
});
// POST ONE
router.post('/books', (req, res, next) => {
const localdb = db.client.db(process.env.DB_NAME);
const collection = localdb.collection(process.env.COLL_BOOKS);
let newBook = req.body;
collection.insertOne(newBook, function (err) {
if (err) throw err
res.status(201).send(true);
});
});
// DELETE ONE
router.delete('/books/:name', (req, res, next) => {
const localdb = db.client.db(process.env.DB_NAME);
const collection = localdb.collection(process.env.COLL_BOOKS);
collection.deleteOne({ "name": req.params.name }, function (err) {
if (err) throw err
res.status(200).send(true)
})
});
// PUT ONE
router.put('/books/:name', (req, res, next) => {
const localdb = db.client.db(process.env.DB_NAME);
const collection = localdb.collection(process.env.COLL_BOOKS);
collection.updateOne({ "name": req.params.name }, { $set: req.body }, function (err) {
if (err) throw err
res.status(201).send(true);
});
});
module.exports = router;
app.js
const express = require('express'),
app = express();
os = require('os');
const bodyParser = require('body-parser');
const cors = require('cors');
const router = require('./router.js')
require('dotenv').config()
app.use(cors());
app.use(bodyParser.json());
app.use('/api/v1', router);
const port = (process.env.PORT || '3001');
let server = app.listen(port, os.hostname(), () => {
let host = server.address().address,
port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
【问题讨论】:
-
您能提供您的代码的sn-p吗? ( /app/database )
-
我还是不能解决问题。如果有人有建议让它发挥作用
标签: javascript node.js mongodb heroku mongodb-atlas