Node.js развертывание при ошибке Heroku: TypeError: невозможно прочитать свойство 'split', равное null - PullRequest
0 голосов
/ 04 мая 2020

Кто-нибудь знает, что означает ошибка? У меня есть приложение на Node.js и моя база данных на Атласе. Когда я пытаюсь развернуть его на Heroku, я получаю это сообщение об ошибке в журналах:

TypeError: невозможно прочитать свойство 'split', равное нулю в parseSrvConnectionString (/ app / node_modules / mongodb / lib / core / uri_parser. js: 40: 23) в parseConnectionString (/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 в mightPromise (/app/node_modules/mongodb/lib/utils.js:719: 3) в MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:214:10) в Function.MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:432: 22) в Database.connect (/app/database.js:9:15) в Object. (/app/database.js:21:4) в Module._compile (internal / modules / cjs / loader. js: 1118: 30) [31m [nodemon] приложение разбилось - ожидание изменений файла перед запуском. .. [39м

База данных. 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;

приложение. 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);
});

1 Ответ

0 голосов
/ 05 мая 2020

Ошибка возникает, когда я удаляю кавычки из DB_URL в конфигурации Heroku var, но теперь появляется другая ошибка:

TypeError: Cannot read property 'db' of null 2020-05-05T10:21:40.450337+00:00 app[web.1]: at /app/router.js:12:31 2020-05-05T10:21:40.450355+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 2020-05-05T10:21:40.450356+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13) 2020-05-05T10:21:40.450356+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3) 2020-05-05T10:21:40.450357+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 2020-05-05T10:21:40.450358+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22 2020-05-05T10:21:40.450358+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12) 2020-05-05T10:21:40.450359+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10) 2020-05-05T10:21:40.450359+00:00 app[web.1]: at Function.handle (/app/node_modules/express/lib/router/index.js:174:3) 2020-05-05T10:21:40.450359+00:00 app[web.1]: at router (/app/node_modules/express/lib/router/index.js:47:12) 2020-05-05T10:21:57.090036+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:725 2020-05-05T10:21:57.090047+00:00 app[web.1]: throw error; 2020-05-05T10:21:57.090048+00:00 app[web.1]: ^ 2020-05-05T10:21:57.090048+00:00 app[web.1]:
...