我知道这个问题一直在问,但我无法弄清楚我的代码在我的代码中做错了什么,在我的' /'当我启动应用程序时,路由器执行我的index.js文件,其中包含以下代码:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Loja = require('../models/lojas');
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date());
console.log('Request Type:', req.method);
console.log('Request URL:', req.originalUrl);
next(); //passa a solicitação para a próxima função de middleware na pilha
});
//get all contacts with specific filter
router.post('/registo',function(req,res){
var loja = new Loja();
loja.name = req.body.name;
loja.email = req.body.email;
loja.setPassword(req.body.password);
loja.save(function(err){
var token;
token = loja.generateJwt;
res.status(200);
res.json({
"token": token
});
});
});
我的app.js如下所示:
var express = require('express');
var bodyParser = require('body-Parser');
var mongoose = require('mongoose');
var passport = require('passport');
require('./config/passport');
var app = express();
var dbName = 'LojasDB';
var connectionString = 'mongodb://localhost:27017' + dbName;
mongoose.Promise = global.Promise;
mongoose.connect(connectionString);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(passport.initialize());
app.use(function(req,res,next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-MethodOverride,Content-Type, Accept');
next();
});
app.use('/',require('./routes/index'));
app.listen(8080,function(){
console.log("listen on port 8080");
})
基本上我有一个名为loja的模型,我想用它进行身份验证,所以我第一次使用护照这样做,但不知何故我得到一个错误,该模式没有被注册为该模型,我的模型看起来像这样:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var jwt = require('jsonwebtoken');
var crypto = require('crypto');
var lojasSchema = mongoose.Schema({
email: {
type: String,
unique: true,
required: true
},
name: {
type: String,
required: true
},
hash: String,
salt: String
});
lojasSchema.methods.generateJwt = function() {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
return jwt.sign({
_id: this._id,
email: this.email,
name: this.name,
exp: parseInt(expiry.getTime() / 1000),
}, "12345"); // DO NOT KEEP YOUR SECRET IN THE CODE!
};
lojasSchema.methods.setPassword = function(password){
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};
lojasSchema.methods.validPassword = function(password) {
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash === hash;
};
module.exports = mongoose.model('Loja',lojasSchema);
所以我在这里导出模型,所以在我的路由器index.js中我需要这样:
var Loja = require('../models/lojas');
答案 0 :(得分:0)
以下一行:
var lojasSchema = mongoose.Schema({
应该是:
var lojasSchema = new mongoose.Schema({
答案 1 :(得分:0)
确保从index.js文件导出路由,因为我看到它们与app.js文件是分开的。您需要在app.js文件中要求使用Mongoose Models。所以在你的app.js中添加这样的东西。
var Loja = require('../models/lojas');