Search code examples
mongodbmongoosemongoose-schema

Mongoose TypeError: Cannot read property 'insertOne' of null


I have been trying to figure out what I am screwing up all morning.

First the problem: When I call myModel.create() I get this in console:

TypeError: Cannot read property 'insertOne' of null

Schema and Model:

const callSchema = new mongoose.Schema({
        date: Date,
        times: {
            dispatch: String,
            clear: String
        },
        incident: Number,
        calltype: String,
        address: {
            streetAddress: String,
            city: String,
            state: String,
            zip: String
        },
        recall: [ 
            {type: mongoose.Schema.Types.ObjectId,
            ref: "User"}],
    });
const callData = mongoose.model("callData", callSchema);

Now for the Data and the Function:

//dummy data:

var call = {
        date: '2020-02-19T08:35:04.803Z', 
        times: {
            dispatch: '1800', 
            clear: '1900'},
        incident: 2000599,
        calltype: 'medical', 
        address: { 
            streetAddress: '1200 Southgate', 
            city: 'Pendleton',
            state: 'OR',
            zip: '97801'},
        recall: ["5e4b5ac03e6a9e3ed521ab80"]
        };

function newCall(runData, times, personell){
    console.log(runData);

    callData.create(runData);
}

newCall(call);

What have I done:

I commented out everything but the date in the schema and in the data to try and narrow down the problem. This did not change any behavior it continued to throw the same error. I have looked around on here and others have had similar issues but I corrected the things that they suggested (changed type: to calltype:), and I cannot find what I messed up on in the schema.

Any help would be appreciated.

-Adam


Solution

  • The issue was in my DB connection. I did not have it setup to wait properly. So this was an asynchronous issue and is now resolved by correctly setting up my connection function to wait and return once complete.

    As an example:

    const mongoose = require('mongoose'),
          logger   = require('../logging');
    
    
    
    async function connect(){
    
    //I had to place mongoose.connect(...)... in side of the const connected.
      const connected =  mongoose.connect("mongodb://localhost:27017/fdData", { 
        useNewUrlParser: true,
        useUnifiedTopology: true,
        bufferCommands: false,
        useFindAndModify: false,
    });
    
      let con = mongoose.connection;
      con.on('error', logger.error.bind(logger, 'connection error:'));
      con.once('open', function() {
        logger.info('DB Connected');
      });
    
      mongoose.connection.on('disconnected', function(){
        console.log("Mongoose default connection is disconnected");
    });
    
    process.on('SIGINT', function(){
      mongoose.connection.close(function(){
          console.log("Mongoose default connection is disconnected due to application termination");
          process.exit(0)
      });
    });
    
    //I had to ADD this return as I did not have it.
    return connected;
    
    }
    
    async function disconnect(){
        await mongoose.disconnect()
    }
    
    module.exports= {connect, disconnect}
    

    By doing those it forces my code to wait to execute the rest until the DB is officially connected.

    -Adam