[使用feathersjs生成服务时,模型文件如下所示
import { Application } from '../declarations';
export default function (app: Application) {
const modelName = 'comments';
const mongooseClient = app.get('mongooseClient');
const { Schema } = mongooseClient;
const schema = new Schema({
text: { type: String, required: true }
}, {
timestamps: true
});
// This is necessary to avoid model compilation errors in watch mode
// see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
if (mongooseClient.modelNames().includes(modelName)) {
mongooseClient.deleteModel(modelName);
}
return mongooseClient.model(modelName, schema);
}
mongooseClient的类型为Mongoose
,文档说deleteModel在Connection.prototype.deleteModel()
下
该行不应该说mongooseClient.connection.deleteModel(modelName);
吗?
不,不应该
[如果您查看根文件夹下的文件mongoose.js
,您会看到'mongooseClient'
在mongoose.connect
之后设置:
const mongoose = require('mongoose');
module.exports = function (app) {
mongoose.connect(
app.get('mongodb'),
{ useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true }
);
mongoose.Promise = global.Promise;
app.set('mongooseClient', mongoose);
};
因此它是Connection.prototype
的一部分。