Sequelize Store在全球范围内查询价值

时间:2016-01-16 21:23:41

标签: node.js express sequelize.js

我试图找出一种方法来存储使用全局变量成功查询的对象,因此每次我想访问和使用该查询中的值时,我都不必查询相同的模型。我基本上继续遇到问题,我尝试传递与organization对象关联的属性值,但是当我尝试在对模型的查询不是实例中使用对象时,我经常得到undefined当下。有什么建议吗?

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;

1 个答案:

答案 0 :(得分:0)

你的问题似乎是双重的。

首先,这一行:

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

这不起作用,快速发帖请求的方法签名不会像这样传递数据。 Express使用中间件,并且在每种情况下组织参数都是未定义的,或者被调用的函数转到堆栈中的下一个中间件。

现在,在这里:

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

这是将组织数据发送到视图的正确方法。但是,您必须将数据存储在html中,以便将其传递到post请求中。

这是通过将数据放在表单内的输入中然后发布到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方法中,您将检索如下数据:

            .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');
                })

            });

注意:要使所有这些工作,您必须使用body-parser中间件进行表达,并将urlencoded扩展集设置为true。

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