Sequelize 全局存储查询的值

Sequelize Store Queried value globally

我正在尝试找到一种方法来存储使用全局变量成功查询的对象,这样我就不必在每次要访问和使用该查询的值时都查询相同的模型。我基本上将 运行 放在我尝试传递与我的 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;

你的问题似乎是双重的。

首先,这一行:

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

这行不通,express post 请求的方法签名不会像那样传递数据。 Express 使用中间件,在每种情况下,组织参数要么是未定义的,要么是一个被调用以转到堆栈中的下一个中间件的函数。

现在,这里:

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

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

这是通过将数据放入表单中的输入来完成的,然后 posted 到 post 方法。 例如,如果组织对象有 say、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 中间件进行 express 并将 urlencoded 扩展设置为 true。

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