Mongoose TypeError: Cannot read property 'insertOne' of null
Mongoose TypeError: Cannot read property 'insertOne' of null
我整个早上都在想我搞砸了什么。
首先是问题:
当我调用 myModel.create() 时,我在控制台中得到了这个:
TypeError: Cannot read property 'insertOne' of null
架构和模型:
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);
现在是数据和函数:
//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);
我做了什么:
我注释掉了架构和数据中除日期之外的所有内容,以尝试缩小问题范围。这并没有改变它继续抛出相同错误的任何行为。我环顾四周,其他人也有类似的问题,但我更正了他们建议的内容(将 type: 更改为 calltype:),但我找不到我在模式中搞砸的地方。
如有任何帮助,我们将不胜感激。
-亚当
问题出在我的数据库连接上。我没有将其设置为正确等待。所以这是一个异步问题,现在通过正确设置我的连接函数等待和 return 一旦完成来解决。
举个例子:
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}
通过执行这些操作,它会强制我的代码等待执行其余部分,直到数据库正式连接。
-亚当
我整个早上都在想我搞砸了什么。
首先是问题: 当我调用 myModel.create() 时,我在控制台中得到了这个:
TypeError: Cannot read property 'insertOne' of null
架构和模型:
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);
现在是数据和函数:
//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);
我做了什么:
我注释掉了架构和数据中除日期之外的所有内容,以尝试缩小问题范围。这并没有改变它继续抛出相同错误的任何行为。我环顾四周,其他人也有类似的问题,但我更正了他们建议的内容(将 type: 更改为 calltype:),但我找不到我在模式中搞砸的地方。
如有任何帮助,我们将不胜感激。
-亚当
问题出在我的数据库连接上。我没有将其设置为正确等待。所以这是一个异步问题,现在通过正确设置我的连接函数等待和 return 一旦完成来解决。
举个例子:
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}
通过执行这些操作,它会强制我的代码等待执行其余部分,直到数据库正式连接。
-亚当