node.js - Sequelize Store Queried value globally -
i trying figure out way store object queried global variable don't have query same model every time want access , use values query. keep running issues try pass values of properties associated organization
object, undefined
when try use object in instances when query model not present. suggestions?
approutes (/sign-up/organization
post captures organization
, /settings/add-user
post comes 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 @ post' + error); }) }); module.exports = approutes;
your problem seems twofold.
first, line:
.post(function(req, res, organization){
this not work, method signature express post requests doesn't pass data in that. express uses middleware , in every case organization argument either undefined or function called go next middleware in stack.
now, here:
res.render('pages/app/add-users.hbs',{ user: req.user, organization: organization });
this correct way send organization data view. must store data in html gets passed post request.
this done putting data in input inside form posted post method. example, if organization object has say, id , name. looks this:
{ organizationid: 'someid', name: 'organization name' }
then data persist must have in 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>
now in post method retrieve data this:
.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'); }) });
note: of work must use body-parser middleware express urlencoded extended set true.
var bodyparser = require('body-parser'); app.use(bodyparser.urlencoded({ extended: true }))
Comments
Post a Comment