我是nodejs的新手,也是我关于stackoverflow的第一个问题 在我的应用程序中,名为passport的数据库有两个存储的集合 用户存储登录信息&买家存储买家信息。 在我的server.js中,我有以下代码
var configDB = require('./config/database.js');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
mongoose.connect(configDB.url);
// connect to our database
require('./config/passport')(passport);
........
........
**My routes.js content are**
var buyer = require('./models/buyer.js');//make sure you write directory path correctly
module.exports = function(app, passport) {
var mongoose = require('mongoose');
// normal routes ===============================================================
// show the home page (will also have our login links)
app.get('/', function(req, res) {
res.render('index.ejs');
});
// locally --------------------------------
// LOGIN ===============================
// show the login form
app.get('/login', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// SIGNUP =================================
// show the signup form
app.get('/signup', function(req, res) {
res.render('signup.ejs', { message: req.flash('signupMessage') });
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// PROFILE SECTION =========================
app.get('/profile', function(req, res) {
res.render('profile.ejs', {
user : req.user
});
});
app.get('/views/transaction.ejs',function(req,res){
return res.render('transaction.ejs');
});
//THIS IS MY SCHEMA FOR TRANSACTION SUBMIT
var buyerSchema = new mongoose.Schema({
Tr_id: String,
//userID : String ,
SellerBank : String,
SellerAccountno: String,
SellerName : String,
Amount:Number
//your_schema
}, { collection : 'buyer'});
var mongoose = require('mongoose');
var buyerModel = mongoose.model('buyer', buyerSchema);
//it is request through form submit button on action /addbulletein
app.post('/addbulletin', function(req, res){
function randomNumber(len) {
var randomNumber;
var n = '';
for(var count = 0; count < len; count++) {
randomNumber = Math.floor(Math.random() * 10);
n += randomNumber.toString();
}
return n;
};
var Tr_id = randomNumber(9);
var Tr_id=Tr_id;
//var userID=user._id,
/// is this correct way to get input ???????????
var SellerBank = req.body.SellerBank;
var SellerAccountno = req.body.SellerAccontno;
var SellerName = req.body.Amount;
//DB INFO : var url='mongodb://localhost/passport'
//trying to insert all the param
db.collection('buyer').insert({
"SellerBank":SellerBank,
"Tr_id":Tr_id,
"SellerBank": SellerBank ,
"SellerAccountno":SellerAccountno,
"SellerName":SellerName
},function(err,doc){
if(err)throw err;
});
});
app.get('/viewall',function(req,res)
{
//now here below buyer is available as a model to use directly
buyerModel.find({},function(err,docs){
if(err){
res.json(err);
}
else{
res.render('display', { users : docs });
}
});
});
// LOGOUT ==============================
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// =============================================================================
// AUTHENTICATE (FIRST LOGIN) ==================================================
// =============================================================================
// locally --------------------------------
// LOGIN ===============================
// show the login form
app.get('/login', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// SIGNUP =================================
// show the signup form
app.get('/signup', function(req, res) {
res.render('signup.ejs', { message: req.flash('signupMessage') });
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// =============================================================================
// AUTHORIZE (ALREADY LOGGED IN / CONNECTING OTHER SOCIAL ACCOUNT) =============
// =============================================================================
// locally --------------------------------
app.get('/connect/local', function(req, res) {
res.render('connect-local.ejs', { message: req.flash('loginMessage') });
});
app.post('/connect/local', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/connect/local', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
//route middleware to ensure user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
};
};
我的buyer.js内容
var mongoose = require('mongoose');
var buyerSchema = new mongoose.Schema({
Tr_id: String,
//userID : String ,
SellerBank : String,
SellerAccountno: String,
SellerName : String,
Amount:Number
//your_schema
}, { collection : 'buyer'});
var buyerModel = mongoose.model('buyer', buyerSchema);
我还有一个调用app.post的html表单('/ addbulletin',function(req,res){
html页面的内容
<html>
<body>
<a href="/trhistory" class="btn btn-default"><span class="fa fa-user"></span>display</a>
<form action="/addbulletin" method="POST">
SellerBank:<br>
<input type="text" name="SellerBank" value="">
<br>
SellerAccountno:<br>
<input type="text" name="SellerAccountno" value="">
<br>
SellerName:<br>
<input type="text" name="SellerName" value="">
<br>
Amount:<br>
<input type="number" name="Amount" value="">
<br>
Application:<br>
<input type="file" id="myFile">
<br><br>
<input type="submit" value="submit transaction">
</form>
</body>
</html>
点击我的html页面的提交按钮时,它会重定向到/ addbuletien并抛出错误ReferenceError:db未定义
答案 0 :(得分:0)
基本上,您只在addbulletin post route中创建买方模型。 您可以做的是在模型文件夹中为买方创建一个模型:
//models/buyer.js
var mongoose = require('mongoose');
var buyerSchema = new mongoose.Schema({
//your_schema
}, { collection : 'buyer'});
var buyerModel = mongoose.model('buyer', buyerSchema);
现在在路由器中导入模型并使用如下所示:
//routes.js
var buyer = require('./models/buyer');//make sure you write directory path correctly
app.get('/viewall',function(req,res)
{
//now here below buyer is available as a model to use directly
buyerModel.find({},function(err,docs){
if(err){
res.json(err);
}
else{
res.render('display', { users : docs });
}
});