2016-01-16 37 views
0

我想找出一種方法來存儲與全局變量成功查詢的對象,所以我不必每次查詢相同的模型,我想要訪問和使用該值時查詢。我基本上一直遇到問題,我試圖傳遞與我的organization對象相關的屬性值,但當我試圖在對模型的查詢不存在時嘗試使用該對象時,我總是得到undefined。有什麼建議麼?Sequelize Store全局查詢值

appRoutes(/sign-up/organization POST捕捉organization,但/settings/add-user POST想出了undefined

var express = require('express'); 
    var appRoutes = express.Router(); 
    var passport = require('passport'); 
    var localStrategy = require('passport-local').Strategy; 
    var models = require('../models/db-index'); 

appRoutes.route('/settings/add-users') 

      .get(function(req, res){ 
       models.Organization.find({ 
        where: { 
         organizationId: req.user.organizationId 
        }, attributes: ['organizationName','admin','members'] 
       }).then(function(organization){ 
        res.render('pages/app/add-users.hbs',{ 
         user: req.user, 
         organization: organization 
        }); 
       }) 
      }) 

      .post(function(req, res, organization){ 
       console.log('Initiated POST Method'); 

       models.Member.create({ 

        organizationId: req.organization.organizationId, 
        memberEmail: req.body.addMember, 

       }).then(function(){ 
        console.log("Success"); 
        res.redirect('/app/settings/add-users'); 
       }) 

      }); 

    appRoutes.route('/sign-up/organization') 

     .get(function(req, res){ 
      models.User.find({ 
       where: { 
        user_id: req.user.email 
       }, attributes: [ 'user_id', 'email' 
       ] 
      }).then(function(user){ 
       res.render('pages/app/sign-up-organization.hbs',{ 
        user: req.user 
       }); 
      }) 
     }) 

     .post(function(req, res, user){ 
      models.Organization.create({ 
       organizationName: req.body.organizationName, 
       admin: req.body.admin 
      }).then(function(organization, user){ 

       models.Member.create({ 
        organizationId: organization.organizationId, 
        memberEmail: req.user.email, 
        userId: req.user.user_id 
       },{ where: { user_id: req.user.user_id }}); 
       res.redirect('/app'); 
      }).catch(function(error){ 
       res.send(error); 
       console.log('Error at Post' + error); 
      }) 
     }); 


    module.exports = appRoutes; 

回答

0

你的問題似乎是雙重的

首先,這條線:

.post(function(req, res, organization){ 

這將不起作用,特快專遞請求的方法簽名不會像這樣傳遞數據。Express使用中間件,而且在任何情況下,組織參數都可能是未定義的,或者是被調用來訪問棧中下一個中間件的函數。

現在,這裏:

res.render('pages/app/add-users.hbs',{ 
    user: req.user, 
    organization: organization 
}); 

這是該組織的數據發送到視圖的正確方法。但是,您必須將數據存儲在您的html中,以便將其傳遞到發佈請求中。

這是通過將數據放入表單中的輸入內部,然後發佈到post方法來完成的。 例如,如果組織對象具有發言權,ID和名稱。看起來是這樣的:

{ 
    organizationId: 'SomeID', 
    Name: 'Organization Name' 
} 

那麼對於數據持久化,你必須有這樣的HTML中:

<form action="/settings/add-users" method="post"> 
    <input type="hidden" name="organization.organizationId" value="SomeId" /> 
    <input type="hidden" name="organization.Name" value="Organization Name" /> 
    <button type="submit">Submit</button> 
</form> 

現在,在您的文章方法,你能獲取這樣的數據:

  .post(function(req, res, organization){ 
       console.log('Initiated POST Method'); 
       console.log(req.body.organization.organizationId); 
       console.log(req.body.organization.Name); 
       models.Member.create({ 
        organizationId: req.body.organization.organizationId, 
        memberEmail: req.body.addMember, 

       }).then(function(){ 
        console.log("Success"); 
        res.redirect('/app/settings/add-users'); 
       }) 

      }); 

注意:爲使所有這些工作正常,您必須使用身體分析器中間件以將urlencoded擴展集合設爲true。

var bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({ extended: true }))