FeathersJS 生成器有错误吗?
Is there a mistake in FeathersJS generator?
用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
的一部分。
用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
的一部分。