Javascript findOne不是功能猫鼬
我知道这是一个老生常谈的问题,我知道我不应该在模型实例上调用Javascript findOne不是功能猫鼬,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我知道这是一个老生常谈的问题,我知道我不应该在模型实例上调用findOne函数,而应该在模型本身上调用 这是我的corporation.model.js: const mongoose = require('mongoose'); const corporationSchema = new mongoose.Schema( { name: { type: String, required: true, trim: true, },
findOne
函数,而应该在模型本身上调用
这是我的corporation.model.js:
const mongoose = require('mongoose');
const corporationSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
},
logo: {
type: String,
required: true,
},
description: {
type: String,
required: false,
trim: true,
},
companyType: {
type: String,
required: true,
},
code: {
type: String,
required: true,
immutable: true,
unique: true,
},
},
{ timestamps: true }
);
corporationSchema.statics.fetchOneWithAssociations = async function fetchOneWithAssociations(
id
) {
// func definition
};
corporationSchema.statics.findByName = async function findByName(text) {
// func definition
};
module.exports = mongoose.model('Corporation', corporationSchema);
const mongoose = require('mongoose');
const { of } = require('await-of');
const Corporation = require('./corporation.model');
const EventSchema = new mongoose.Schema(
{
title: { type: String, required: true },
subtitle: { type: String },
corporationId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Corporation',
required: true,
},
},
{ timestamps: true }
);
EventSchema.statics.findByCorporationId = async function findByCorporationId(
corporationId,
filterQuery,
limit,
offset,
sort
) {
try {
let corporation = {};
let logoErr = {};
let totalEvents = 0;
let events = [];
if (corporationId) {
[totalEvents, errCount] = await of(
this.count({ corporationId, ...filterQuery })
);
[events, err] = await of(
this.find({ corporationId, ...filterQuery })
.sort(sort)
.limit(limit)
.skip(offset)
);
console.log('Corporation', Corporation);
[corporation, logoErr] = await of(
Corporation.findOne({ _id: corporationId }, 'logo')
);
if (err || errCount) {
throw err;
}
}
return {
events,
count: totalEvents,
logo: corporation.logo,
};
} catch (e) {
throw e;
}
};
EventSchema.statics.exceptByCorporationId = async function exceptByCorporationId(
corporationId,
filterQuery,
limit,
offset,
sort
) {
// func definition
};
module.exports = mongoose.model('Event', EventSchema);
还有我的event.model.js:
const mongoose = require('mongoose');
const corporationSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
},
logo: {
type: String,
required: true,
},
description: {
type: String,
required: false,
trim: true,
},
companyType: {
type: String,
required: true,
},
code: {
type: String,
required: true,
immutable: true,
unique: true,
},
},
{ timestamps: true }
);
corporationSchema.statics.fetchOneWithAssociations = async function fetchOneWithAssociations(
id
) {
// func definition
};
corporationSchema.statics.findByName = async function findByName(text) {
// func definition
};
module.exports = mongoose.model('Corporation', corporationSchema);
const mongoose = require('mongoose');
const { of } = require('await-of');
const Corporation = require('./corporation.model');
const EventSchema = new mongoose.Schema(
{
title: { type: String, required: true },
subtitle: { type: String },
corporationId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Corporation',
required: true,
},
},
{ timestamps: true }
);
EventSchema.statics.findByCorporationId = async function findByCorporationId(
corporationId,
filterQuery,
limit,
offset,
sort
) {
try {
let corporation = {};
let logoErr = {};
let totalEvents = 0;
let events = [];
if (corporationId) {
[totalEvents, errCount] = await of(
this.count({ corporationId, ...filterQuery })
);
[events, err] = await of(
this.find({ corporationId, ...filterQuery })
.sort(sort)
.limit(limit)
.skip(offset)
);
console.log('Corporation', Corporation);
[corporation, logoErr] = await of(
Corporation.findOne({ _id: corporationId }, 'logo')
);
if (err || errCount) {
throw err;
}
}
return {
events,
count: totalEvents,
logo: corporation.logo,
};
} catch (e) {
throw e;
}
};
EventSchema.statics.exceptByCorporationId = async function exceptByCorporationId(
corporationId,
filterQuery,
limit,
offset,
sort
) {
// func definition
};
module.exports = mongoose.model('Event', EventSchema);
行console.log('Corporation',Corporation)代码>打印对象,即{}
。好的,我明白,这是个问题,但这是我的问题。为什么?我的意思是,似乎我已经正确地创建了我的模型。我没有。为什么我把模型作为实例而不是模型类本身