2017-05-25 83 views
1

下午好!我一直在建立一個小的聯繫人列表應用程序,我想用貓鼬來存儲數據和會話項目。我之前從未使用貓鼬,所以我選擇了一個快速的PDF以跟隨。大部分如果有意義,我可以將一些東西拼湊在一起,並使用UserSchema來驗證身份,以添加新用戶。很棒。我現在有點迷失了,我怎麼能用貓鼬創建第二個模式,稱爲ContactSchema。我以爲我會爲每個模式(user.js和contact.js)創建2個不同的文件,但是當我嘗試時,我仍然沒有看到聯繫人集合,當我在我的mongo終端中運行show collections,而且我也沒有拋出任何錯誤。當我在robomongo中檢查它時也是如此。貓鼬:創建用戶綱要和待辦綱要

我的服務器文件調用所有我的模型是這樣的:

require('./server/models').connect(config.dbUri); 
// config.dbUri -> "dbUri": "mongodb://localhost/react_app" 

我user.js的文件:

const mongoose = require('mongoose'); 
const bcrypt = require('bcrypt'); 

// define the User model schema 
const UserSchema = new mongoose.Schema({ 
    email: { 
    type: String, 
    index: { unique: true } 
    }, 
    password: String, 
    name: String 
}); 

...bcrypt code here 

module.exports = mongoose.model('User', UserSchema); 

我contact.js文件:

const mongoose = require('mongoose'); 

// define the Contact model schema 
const ContactSchema = new mongoose.Schema({ 
    content : String, 
    updated_at : Date 
}); 

module.exports = mongoose.model('Todo', ContactSchema); 

我的指數.js文件需要user.js和contact.js模式:

const mongoose = require('mongoose'); 

module.exports.connect = (uri) => { 
    mongoose.connect(uri); 

    // plug in the promise library 
    mongoose.Promise = global.Promise; 

    mongoose.connection.on('error', (err) => { 
    console.error(`Mongoose connection error: ${err}`); 
    process.exit(1); 
    }); 

    require('./user'); 
    require('./contact'); 
}; 

index.js,user.js和contact.js文件都在我的模型文件夾中。我可以讓它在控制檯中沒有錯誤的情況下運行,但我仍然沒有看到mongo端的聯繫人集合。我在正確的軌道上嗎?與往常一樣,任何幫助或相關帖子都非常感謝。爲了獲得更好的想法,下面是完整項目的github鏈接:https://github.com/DanDeller/Base。再次感謝你們。

回答

3

您是否已將任何數據添加到聯繫人集合?

Mongoose是一種提供映射到MongoDB集合的抽象模式的'ORM'。但是,這些模式僅存在於使用貓鼬的應用程序的上下文中。 MongoDB沒有模式概念。這是一個相當不錯的文件存儲。它不像SQL,在你的ORM中定義一些東西通常會在DB中創建一個物理表。 mongoose提供的模式純粹是抽象的,只在應用程序級別執行。

直到您告訴您的應用程序根據您的聯繫方案創建記錄並指示貓鼬將其保存到商店,mongo將不知道聯繫。

+0

感謝提示@fluoresce。這就說得通了。我會嘗試添加一些項目,看看我能想出什麼。 –