下午好!我一直在建立一个小的联系人列表应用程序,我想用猫鼬来存储数据和会话项目。我之前从未使用猫鼬,所以我选择了一个快速的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。这就说得通了。我会尝试添加一些项目,看看我能想出什么。 –