Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

javascript - TypeError: Cannot read property 'split' of undefined, When using a .env file and configuring it

const mongoose = require('mongoose');
const classes = require('./classes');

class Database {

  constructor(client) {
    this.client = client;

    if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
    if(!process.env.ADMIN_USERS.split(' ').includes('229285505693515776')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 229285505693515776";

    for (const i in classes) {
      this[i.toLowerCase()] = new classes[i](this.client, this, require(`@models/${i.toLowerCase()}.js`));
      //Object.assign(this[i.toLowerCase()], { fn: new classes[i](this.client, this), modal: require(`./models/${i.toLowerCase()}.js`) })
      //Object.assign(this.models, { [i.toLowerCase()]: require(`./models/${i.toLowerCase()}.js`) });
    }
  }
}

module.exports = Database;

This is the code. When I execute it, I get this error

    if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
                                ^

TypeError: Cannot read property 'split' of undefined
    at new Database (/home/runner/VoidServers/bot/database/Database.js:16:33)
    at new VoidBot (/home/runner/VoidServers/bot/base/BotClass.js:13:21)
    at Object.<anonymous> (/home/runner/VoidServers/index.js:5:16)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Any help will be really appreciated. I tried editing the file like many times and I failed all the time.

question from:https://stackoverflow.com/questions/65869748/typeerror-cannot-read-property-split-of-undefined-when-using-a-env-file-and

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Install dotenv:

npm install dotenv

and add the line below at the beginning of your code:

require('dotenv').config()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...