我一直在研究这个问题太多时间无济于事。我只是想更新用户数据,但似乎是我不理解的东西。在app.post(..);我觉得我正在正确处理问题,但我得到“未定义不是一个函数”,其中“var activity = new activity({title:req.body [”activities.title“]});”。我迷路了...请帮助!
var passport = require('passport');
var Account = require('./models/user');
var mongoose = require('mongoose');
var LocalStrategy = require('passport-local').Strategy;
var connect = require('connect');
var activity = require('mongoose').model('activity').schema;
module.exports = function (app, models) {
app.get('/home', function (req, res) {
if (req.user){ console.log('userin');
}
res.render('home', { user : req.user });
});
app.get('/register', function(req, res) {
res.render('register', { });
});
app.post('/register', function(req, res) {
Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) {
if (err) {
return res.render("register", {info: "Sorry. That username already exists. Try again."});
}
passport.authenticate('local')(req, res, function () {
res.redirect('/');
});
});
});
app.get('/schedule', function(req, res) {
res.render('schedule', { user : req.user });
});
app.post('/schedule', function(req, res){
var activity = new activity({title: req.body["activities.title"]});
activity.user = req.user;
activity.save(function(err){
if(!err){
res.redirect('/schedule');
}
else{
res.redirect('/');
}
});
});
app.get('/login', function(req, res) {
res.render('login', { user : req.user });
});
app.get('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/schedule');
});
app.post('/login',
passport.authenticate('local', { successRedirect: '/schedule',
failureRedirect: '/login' })
);
// in case of depreciation
/*app.post('/login', passport.authenticate('local'), function(req, res) {
res.redirect('/');
});*/
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/ping', function(req, res){
res.send("pong!", 200);
});
};
以下是我的模型文件
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
passportLocalMongoose = require('passport-local-mongoose')
var Schema = mongoose.Schema;
var location = new Schema({
state: String,
city: String,
location: String
});
var user = new Schema({
activities: [activities]
},
{collection:'user'});
var activities = new Schema ({
title: String,
description: String,
location: [location],
time: String,
date: String,
invite:[user],
notes: [notes]
});
var notes = new Schema({
notes: String
});
user.plugin(passportLocalMongoose);
//export schema model .........> name schemas
module.exports = mongoose.model('user', user);
var activity = mongoose.model('activity', activities);
我的app文件如下:
var express = require('express');
var fortune = require('./lib/fortune.js');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var session = require('express-session');
var mongoose = require('mongoose');
var Account = require('./models/user');
var connect = require('connect');
var bodyParser = require('body-parser');
var app = express();
// set up handlebars
var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main', helpers: {
section: function(name, opions){
if(!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null
}
}
});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session());
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + ';press Ctrl-C to terminate.');
});
app.use(function(req, res, next){
res.locals.showTests = app.get('env') !== 'production' &&
req.query.test === '1';
next();
});
app.use(function(req, res, next){
if(!res.locals.partials) res.locals.partials = {};
res.locals.partials.weather = getWeatherData();
next();
});
app.get('/about', function(req, res) {
res.render('about', {
fortune: fortune.getFortune(),
pageTestScript: '/qa/tests-about.js'} );
});
app.get('/schedule', function(req, res){
res. render('schedule', {csrf: 'CSRF token goes here'});
});
app.post('/process', function(req, res){
console.log('Form (from querysting):' + req.queryform);
console.log('CSRF token (from hidden form field): ' + req.body._csrf);
console.log('Name (from visible form field): ' + req.body.name);
console.log('Email (from visible form field): ' + req.body.email);
res.redirect(303, '/thank-you');
});
app.get('/tours/rivercity', function(req, res){
res.render('tours/rivercity');
});
app.get('/tours/oregonhill', function(req,res){
res.render('tours/oregonhill');
});
app.get('/tours/request-group-rate', function(req, res){
res.render('tours/request-group-rate');
});
app.get('/', function(req,res){
res.render('home');
});
app.get('/home', function(req,res){
res.render('home');
});
app.get('/about', function(req, res){
res.render('about', {fortune: fortune.getFortune()} );
});
// routes
require('./routes')(app);
//custom 404 page
app.use(function(req, res, next){
res.status(404);
res.send('404 - Not Found');
});
// 500 error handler (middleware)
app.use(function(err, req, ers, next){
console.error(err.stack);
res.status(500);
res.render('500');
});
//custom 404 page
app.use(function(req, res){
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
//custom 500 page
app.use(function(err, req, res, next){
console.error(err.stack);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
var User = require('./models/user');
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
mongoose.connect('mongodb://localhost/stoosh');
我正在使用如下所示的把手模板引擎
<form role='form' action="/schedule" method="POST" >
title:<br>
<input type="text" name="activities[title]">
<br>
<input type="submit" value="Submit">
</form>
答案 0 :(得分:0)
您宣布&#34;活动&#34;作为变量两次
var activity = require('mongoose').model('activity').schema;
// blah, blah, blah
app.post('/schedule', function(req, res){
var activity = new activity({title: req.body["activities.title"]});
&#34; new&#34;命令正在应用于新声明的活动变量而不是先前声明的活动变量。而是尝试,
app.post('/schedule', function(req, res){
var newActivity = new activity({title: req.body["activities.title"]});