2017-02-12 66 views
0

試圖創建新的貓鼬模式,但問題是我總是隻有兩列獲得新集合:_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; 

一旦我試圖創建新的用戶它創建新文檔在數據庫,但只有兩個默認列...

+0

嗯,我認爲沒關係,因爲我正在相應地導入它。無論如何,我試圖改變名稱,但仍然沒有成功,只收集了兩個字段。 –

回答

1

我的錯,代碼是o k,但問題是在谷歌瀏覽器中通過郵遞員擴展將值發佈到服務器。只需要添加Content-Type = application/json。而已。 猜猜我現在可以關閉這個問題。