gpt4 book ai didi

javascript - 仅定义 mongoose 和 Schema

转载 作者:行者123 更新时间:2023-11-30 17:44:31 24 4
gpt4 key购买 nike

您好,我正在使用 express 和 mongodb 开发 nodejs 应用程序。所以我必须在我的所有路由 js 中定义 mongoose 和 schema 才能使用 mongo 和 schema。我只想定义它们。我是 node.js 的新手,所以请耐心等待。我的项目结构:

enter image description here

我的route.js如下图所示:

var routes = function (app) {
app.locals.error=null;
app.get('/login', function (req, res) {
return res.render(__dirname + "/views/login", {
title: 'Giriş',
stylesheet: 'login'
});
});
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/fuatblog");
var UserSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
age: Number
}),

Users = mongoose.model('Users', UserSchema);
app.post('/sessions', function (req, res) {
console.log(req.body.login.email);
console.log(req.body.login.password);
console.log(req.body.login.rememberMe);
Users.find({
email: req.body.login.email,
password: req.body.login.password
}, function (err, docs) {
if (! docs.length) {
// no results...

console.log('User Not Found');
res.status(400);

return res.render(__dirname + "/views/login", {
title: 'Giriş',
stylesheet: 'login',
error: 'Kullanıcı adı veya şifre yanlış'
});
}

console.log('User found');
req.session.email = docs[0].email;
console.log(req.session.email);
return res.redirect('/Management');
});
});
};

module.exports = routes;

还有我的 server.js(app.js)

/**
* Module dependencies.
*/
////Mongoose eklendi
var express = require('express'),
mongoose= require('mongoose');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
////Cookie için eklendi.
app.use(express.cookieParser());
////Session desteği için eklendi
app.use(express.session({secret: 'asdfsdfsafasdfasdfasdf'}));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
////Put ve Delete mothodları için
app.use(express.methodOverride());
////Requeestleri ayrıştırmak için kullanılıyor
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));


// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
////Helpers
//require('./apps/helpers')(app);
//Routes

require('./apps/authentication/routes')(app)
require('./apps/blog/routes')(app)
require('./apps/management/routes')(app)
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

最佳答案

我假设您的意思是您只想定义一次架构。

我喜欢做的是在服务器启动并建立 mongodb 连接时初始化我将要使用的所有模型。

理想的目录结构应该是这样的:

mongodb
--schemas
----user.js
--models.js

您可以将您的架构放在架构文件夹中,例如您的用户模型:

(user.js)    
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports = function() {
var UserSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
age: Number
});

mongoose.model("User", UserSchema);
};

在 models.js 中,您将拥有初始化模式目录中每个模式模型的代码。

(models.js)    
exports.initialize = function() {
require("fs").readdirSync(__dirname + "/schemas").forEach(function(file) {
require('./schemas/' + file)();
});
};

在您的 app.js 文件中,您将在建立与 mongoose 的连接后执行此调用以初始化所有模式:

require($pathToYourModelsJsFile).initialize();

在此之后,您就可以使用您的模型了!当你想使用它们时,你所要做的就是:

var mongoose = require('mongoose');
var User = mongoose.model('User');

// Do work
User.find();

关于javascript - 仅定义 mongoose 和 Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379665/

24 4 0
文章推荐: javascript - 使用 jQuery 在