下午好!我一直在建立一個小的聯繫人列表應用程序,我想用貓鼬來存儲數據和會話項目。我之前從未使用貓鼬,所以我選擇了一個快速的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。再次感謝你們。
感謝提示@fluoresce。這就說得通了。我會嘗試添加一些項目,看看我能想出什麼。 –