我有一個快遞服務器與Passport本地stategy,passport-local和passport-local-mongoose也installed.I要訪問當前用戶模型爲json讀取和寫入數據到它。我頭疼的是該對象不想在我的視圖中顯示數據。它加載沒有錯誤,我不明白髮生了什麼。請幫幫我!req.user數據不顯示在視圖
這裏是我的快遞配置:
var express = require('express'),
app = express(),
morgan = require('morgan'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
expressSession = require('express-session'),
hash = require('bcrypt-nodejs'),
mongoose = require('mongoose'),
path = require('path'),
passport = require('passport'),
localStrategy = require('passport-local').Strategy,
port = process.env.PORT || 8080,
User = require('./app/models/user'),
routes = require('./app/routes/api');
mongoose.connect('mongodb://localhost:27017/dashboard');
app.use(express.static(__dirname + '/src'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(require('express-session')({
secret: 'keyboard kat',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET', 'POST');
res.header('Access-Control-Allow-Methods', 'X-Requested-With, content- type, Authorization');
next();
});
app.use('/user', routes);
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/src/views', 'index.html'));
});
app.use(function (req, res, next) {
var err = new Error('not found :(');
err.status = 404;
next(err);
});
app.use(function (err, req, res) {
res.status(err.status || 500);
res.end(JSON.stringify({
message: err.message,
error: {}
}));
});
app.listen(port);
console.log('Magic happens on port ' + port);
,路線,我用它來提取數據:
router.get('/data', function (req, res) {
res.send(req.user);
});
Сhrome開發工具返回正確的對象:
有角服務:
angular.module('main')
.factory('MenuService', function ($http) {
function getData() {
$http.get('/user/data')
.success(function (data) {
return data;
});
}
return {
info: getData
}
});
控制器:
angular.module('main')
.controller('menuController', function ($mdSidenav, MenuService) {
var vm = this;
vm.toggleMenu = function() {
$mdSidenav('menu').toggle();
};
vm.info = MenuService.info();
});
而且finaly的觀點:
<md-toolbar layout="row" ng-controller="menuController as menu">
<div class="md-toolbar-tools">
<md-button class="md-icon-button md-primary" ng-click="menu.toggleMenu()" aria-label="open menu">
<md-icon md-svg-icon="../../assets/img/hmg.svg"></md-icon>
</md-button>
<h1>Dashboard</h1>
<span flex></span>
<h1>{{menu.info.name}}</h1>
</div>
</md-toolbar>
並沒有任何反應:(無景觀,沒有錯誤,絕對沒有:
謝謝你的回答,@eenagy!我按照你的寫法制作,但仍然沒有任何反應: –
此處導航的結果是什麼http:// localhost:8080/user/data? – eenagy
這裏:[郵遞員](http://i.imgur.com/sqhgtHj .jpg?1)和[瀏覽器](http://i.imgur.com/LwZZ11B.jpg?1) –