我想请你帮忙。
在localhost上运行的应用程序很棒。 但是在服务器上没有添加" isAdmin" :true,到mongoDB。当您添加admin。
为什么呢?问题在哪里?
本地主机:
{
"_id" : ObjectId("57b174723e755f5dfe36d8b0"),
"salt" : "63855ff664682ad1e8ed77ded97ca5b92da472e9f0f7d68dcb058f35e71d38a4",
"hash" : "03da57be47c9ab1c9657ae9cd5ac3d8b63d56c808e28b51d6f5166bf27d8df99a2e6c25d809fcbd2436b5b883b4a64bd835ea588348d920346d9b3b601c965b90ff23b67687118b56a2b3e35343eb4b693131f7f51f7d1c9bdb4989364477b237f49d496505592564a5c3d88a0b559dc65e5543df06e4c22da50589e6551b69c2406db093a7ef78d31bc8bb8a5423ed4b677913642e0cd335992d222c49e5c58c3450068bd5a2e1cab82a9f73829f695b4686bc76f52c76e0b6ea4f248cb7e8663a96900e5d845773f3a4f09f7988a6ae24fbbdbb0ca7e670a51acd3f9b8c06f533b8c851c40680bd7156d00fe1407acf4879d8095591e8dce3a5379e041a90acb04edecafb38b0093e20db5dc41cd803ae70f351c9e8146d0e959d10114e586a370cd8063e47cc29367af9574e1a20d3973ab4be8d5a16b8a35d89c3534cdf745adfc65cd1d769811a421ea9654884dee289807e518b7eb7ba4c3e5f59242f98df6ccb3f09a9824e8679aec579a8c9a1498fc5819a2e1e8ab2f3cbf866f0e736e5c0b855d9d0f80b462fd50bf7ebf402530aaed84d6f7db5885098124ffa225563517c276563fd7eb3b058cb1f2472896a0b322bdd3b552229a677c45847667b952807ab873e5d2356297514c85cd4c3b3fac3bc3ac16d93033546fa9096e4b738f7eabd1c3494f902d0817b977116f612b3ee9040e0f9cab7e35543a42dc30",
"username" : "mail@gmail.com",
"displayName" : "Admin",
"isAdmin" : true,
"__v" : 0
}
服务器:
{
"_id" : ObjectId("57b174723e755f5dfe36d8b0"),
"salt" : "63855ff664682ad1e8ed77ded97ca5b92da472e9f0f7d68dcb058f35e71d38a4",
"hash" : "03da57be47c9ab1c9657ae9cd5ac3d8b63d56c808e28b51d6f5166bf27d8df99a2e6c25d809fcbd2436b5b883b4a64bd835ea588348d920346d9b3b601c965b90ff23b67687118b56a2b3e35343eb4b693131f7f51f7d1c9bdb4989364477b237f49d496505592564a5c3d88a0b559dc65e5543df06e4c22da50589e6551b69c2406db093a7ef78d31bc8bb8a5423ed4b677913642e0cd335992d222c49e5c58c3450068bd5a2e1cab82a9f73829f695b4686bc76f52c76e0b6ea4f248cb7e8663a96900e5d845773f3a4f09f7988a6ae24fbbdbb0ca7e670a51acd3f9b8c06f533b8c851c40680bd7156d00fe1407acf4879d8095591e8dce3a5379e041a90acb04edecafb38b0093e20db5dc41cd803ae70f351c9e8146d0e959d10114e586a370cd8063e47cc29367af9574e1a20d3973ab4be8d5a16b8a35d89c3534cdf745adfc65cd1d769811a421ea9654884dee289807e518b7eb7ba4c3e5f59242f98df6ccb3f09a9824e8679aec579a8c9a1498fc5819a2e1e8ab2f3cbf866f0e736e5c0b855d9d0f80b462fd50bf7ebf402530aaed84d6f7db5885098124ffa225563517c276563fd7eb3b058cb1f2472896a0b322bdd3b552229a677c45847667b952807ab873e5d2356297514c85cd4c3b3fac3bc3ac16d93033546fa9096e4b738f7eabd1c3494f902d0817b977116f612b3ee9040e0f9cab7e35543a42dc30",
"username" : "mail@gmail.com",
"displayName" : "Admin",
"__v" : 0
}
模型
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var User = new Schema({
username: String,
password: String,
displayName: String,
isAdmin: Boolean
}, {
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
User.virtual("token").set(function(token) {
this._token = token;
}).get(function() { return this._token; });
User.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", User);
RegisterController
app.controller("RegisterController",
[
"$scope", "$location", "Account", "Session",
function($scope, $location, account, session) {
$scope.registerForm = {
name: undefined,
email: undefined,
password: undefined,
confirmPassword: undefined,
errorMessage: undefined
};
$scope.register = function() {
account.register($scope.registerForm.name, $scope.registerForm.email, $scope.registerForm.password, $scope.registerForm.confirmPassword)
.then(function(res) {
session.setUserData(res);
$location.path("/");
}, function(response) {
$scope.registerForm.errorMessage = response.message;
});
};
}
]);
服务会话
app.factory("Session", ["$http", function ($http) {
return {
getToken: function() {
var value = sessionStorage.getItem("token");
if (value) return value;
return undefined;
},
getEmail: function() {
var value = sessionStorage.getItem("email");
if (value) return value;
return undefined;
},
getIsAdmin: function() {
var value = sessionStorage.getItem("isAdmin");
if (value) return value == "true";
return undefined;
},
setUserData: function(user) {
sessionStorage.setItem("token", user.token);
sessionStorage.setItem("email", user.username);
$http.defaults.headers.common["Authorization"] = "Bearer " + user.token;
if (user.isAdmin) {
sessionStorage.setItem("isAdmin", "true");
}
},
clear: function() {
sessionStorage.clear();
$http.defaults.headers.common["Authorization"] = undefined;
}
};
}]);