简体   繁体   English

Node.js 在 Heroku 上部署失败:TypeError:无法读取 null 的属性“拆分”

[英]Node.js deploy on Heroku fail : TypeError: Cannot read property 'split' of null

Does anyone know what the error means?有谁知道错误是什么意思? I have my app on Node.js and my database on Atlas.我在 Node.js 上有我的应用程序,在 Atlas 上有我的数据库。 When I try to deploy it on Heroku I get this error message in the logs:当我尝试在 Heroku 上部署它时,我在日志中收到以下错误消息:

TypeError: Cannot read property 'split' of null at parseSrvConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:40:23) at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:556:12) at connect (/app/node_modules/mongodb/lib/operations/connect.js:272:3) TypeError:无法在 parseSrvConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:40:23)处读取 null 的属性“拆分”(/app/node_modules/mongodb/lib/core/uri_parser.js: 556:12) 在连接 (/app/node_modules/mongodb/lib/operations/connect.js:272:3)
at /app/node_modules/mongodb/lib/mongo_client.js:218:5 at maybePromise (/app/node_modules/mongodb/lib/utils.js:719:3) at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:214:10) at Function.MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:432:22) at Database.connect (/app/database.js:9:15) at Object.在 /app/node_modules/mongodb/lib/mongo_client.js:218:5 在 MaybePromise (/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) at Module._compile (internal/modules/cjs/loader.js:1118:30) [31m[nodemon] app crashed - waiting for file changes before starting...[39m (/app/database.js:21:4) 在 Module._compile (internal/modules/cjs/loader.js:1118:30) [31m[nodemon] 应用程序崩溃 - 在开始之前等待文件更改...[39m

Database.js数据库.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;

router.js路由器.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应用程序.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);
});

I got the same problem and solved it by not storing the complete string of the mongo Uri in the.env file.我遇到了同样的问题并通过不在 .env 文件中存储mongo Uri的完整字符串来解决它。 instead, I only Stored the user and the password相反,我只存储了用户和密码

./config.env ./config.env

DB_USER=youruser
DB_PASS=yourpassword

./app.js ./app.js

mongoose.connect(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.pm4ek.gcp.mongodb.net/CommentsDB?retryWrites=true&w=majority`)

The error goes when I remove the quotes from the DB_URL in Heroku var config, but now it brings me an other error, which is:当我从 Heroku var config 中的 DB_URL 中删除引号时出现错误,但现在它给我带来了另一个错误,即:

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]:

I removed quotes from docker-compose yml file and it works for me now.我从 docker-compose yml 文件中删除了引号,它现在对我有用。

I was facing the same problem and this link helped me to solve the error.我遇到了同样的问题, 这个链接帮助我解决了这个错误。

TLDR; TLDR; You should remove double quote on your mongo URI config var您应该删除 mongo URI config var 上的双引号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB Node.js TypeError:无法读取 null 的属性“db” - MongoDB Node.js TypeError: Cannot read property 'db' of null 未捕获的TypeError:无法读取null Node.js的属性'getContext' - Uncaught TypeError: Cannot read property 'getContext' of null Node.js node.js/discord.js:类型错误:无法读取 null 的属性“setPresence” - node.js/discord.js: TypeError: Cannot read property 'setPresence' of null 在 Node.js 中导出 ZCCADCDEDB567ABAE643E15DCF0974E503Z 方法(类型错误:无法读取 null 的属性“名称”) - Exporting Mongoose methods in Node.js (TypeError: Cannot read property 'name' of null) Node.JS HTTPS问题TypeError:无法读取null的属性'writeQueueSize' - Node.JS HTTPS issue TypeError: Cannot read property 'writeQueueSize' of null Heroku上的Node.js,无法读取未定义的属性“ env” - Node.js on heroku, Cannot read property 'env' of undefined Node.js 抛出 TypeError:无法读取未定义的属性“testModule” - Node.js throwing TypeError: Cannot read property 'testModule' of undefined TypeError:无法读取未定义的属性“ get”(Node.js) - TypeError: Cannot read property 'get' of undefined (Node.js) TypeError:无法读取node.js中未定义的属性“名称” - TypeError: Cannot read property 'name' of undefined in node.js Node.js UnhandledPromiseRejection:TypeError:无法读取未定义的属性“ sign” - Node.js UnhandledPromiseRejection: TypeError: Cannot read property 'sign' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM