[英]Creating schema with mongoose and typescript
試圖創建新的貓鼬模式,但問題是我總是得到只有兩列的新集合:_id 和 __v。 就是這樣,模式中沒有我的列。 這是架構代碼:
import DataAccess from '../DataAccess';
import IUserModel from '../../model/interfaces/UserModel';
var mongoose = DataAccess.mongooseInstance;
var mongooseConnection = DataAccess.mongooseConnection;
class UserSchema {
static get schema() {
var schema = mongoose.Schema({
email: {
type: String,
required: false
},
firstName: {
type: String,
required: false
},
lastName: {
type: String,
required: false
},
createdAt: {
type: Date,
required: false
}
});
return schema;
}
}
var schema = mongooseConnection.model<IUserModel>('Users', UserSchema.schema);
export default schema;
用戶模型非常簡單:
import mongoose = require('mongoose');
interface UserModel extends mongoose.Document {
email: string;
firstName: string;
lastName: string;
createdAt: Date;
}
export default UserModel;
數據訪問層:
import mongoose = require('mongoose');
class DataAccess {
static mongooseInstance: any;
static mongooseConnection: mongoose.Connection;
static connect(): mongoose.Connection {
const MONGODB_CONNECTION: string = 'mongodb://localhost:27017/dbname';
if (this.mongooseInstance) return this.mongooseInstance;
this.mongooseConnection = mongoose.connection;
this.mongooseConnection.once('open', () => {
console.log('Connected to mongodb');
})
this.mongooseInstance = mongoose.connect(MONGODB_CONNECTION);
return this.mongooseInstance;
}
}
DataAccess.connect();
export default DataAccess;
一旦我嘗試創建新用戶,它就會在數據庫中創建新文檔,但只有兩個默認列......
我的錯,代碼沒問題,但問題在於通過 google chrome 中的郵遞員擴展將值發布到服務器。 只需添加 Content-Type = application/json。 就是這樣。 猜猜我現在可以關閉這個問題了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.