2017-01-25 47 views
0

sq.js:Sequelize Model類引用錯誤

var Sequelize = require('sequelize'); 
var sequelize = new Sequelize('postgres://chandan:[email protected]:5432/diary'); 

var User = sequelize.define('user', { 
    username: { 
    type: Sequelize.STRING, 
    field: 'username' // Will result in an attribute that is firstName when user facing but first_name in the database 
    }, 
    password: { 
    type: Sequelize.STRING 
    } 
},{ 
timestamps: false, 
}); 

User.sync({force: true}).then(function() { 
    // Table created 
}); 

module.exports = User; 

app.js:

var seq = require('./routes/sq'); 

passport.use(new passportLocal.Strategy(function(username,password, done){ 
    User.findOne({username: username}, function (err, user) { 
     if (err) { return done(err); } 
     if (!user) { 
     return done(null, false, { message: 'Incorrect username.' }); 
     } 
     if (!user.validPassword(password)) { 
     return done(null, false, { message: 'Incorrect password.' }); 
     } 
     return done(null,{ id:username, name:username}); 
    }); 
})); 

我收到錯誤:的ReferenceError:用戶沒有定義

回答

3

其因爲你已經在app.js中聲明瞭用戶模型seq。你的聲明更改爲:

var User = require('./routes/sq'); 

變量User只住在sq.js

+0

沒錯奏效......但現在的查詢不執行......它顯示在控制檯as .......執行(默認):SELECT「id」,「username」,「password」FROM「users」AS「user」LIMIT 1; –