【问题标题】:Sequelize Store Queried value globallySequelize 全局存储查询值
【发布时间】:2016-01-23 22:35:50
【问题描述】:

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

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    您的问题似乎是双重的。

    首先,这一行:

    .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 方法中,您可以像这样检索数据:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      相关资源
      最近更新 更多