Sending sequelize results through express after forEach loop has run
I have a somewhat complicated sequelize query running in an express route, and I have a need to run a second query afterwards, to append needed data to the returned result:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF' },
include: [
{ model: models.form, as: 'form' },
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
response.forEach( item ) => {
models.employees.findAll({
attributes: [ 'fname', 'mname', 'lname' ],
where: { empl_id: item.form[0].empl_id },
order: [ 'empl_id' ]
}).then(function(response){
item.readableName = response;
});
});
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
The issue I'm having here (with the current setup) is that I need the forEach to finish appending data before I call res.send(response). If I take the last .then() off of this code and put res.send in the same block as the loop, the rest of the data gets sent correctly but no results from the employees table show. However, with the way it is set up above, nothing is sent to the response object that I am trying to send.
My question is twofold
First:
The employees table doesn't have any columns that match the original table (fas). Because of that, it's my understanding that I can't make an association between the two (which would get rid of the need for the forEach). Is that true?
Second:
If the loop is the right way to do this, how could I return the results from the forEach into the next .then and send it using res.send()?
javascript node.js express foreach sequelize.js
add a comment |
I have a somewhat complicated sequelize query running in an express route, and I have a need to run a second query afterwards, to append needed data to the returned result:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF' },
include: [
{ model: models.form, as: 'form' },
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
response.forEach( item ) => {
models.employees.findAll({
attributes: [ 'fname', 'mname', 'lname' ],
where: { empl_id: item.form[0].empl_id },
order: [ 'empl_id' ]
}).then(function(response){
item.readableName = response;
});
});
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
The issue I'm having here (with the current setup) is that I need the forEach to finish appending data before I call res.send(response). If I take the last .then() off of this code and put res.send in the same block as the loop, the rest of the data gets sent correctly but no results from the employees table show. However, with the way it is set up above, nothing is sent to the response object that I am trying to send.
My question is twofold
First:
The employees table doesn't have any columns that match the original table (fas). Because of that, it's my understanding that I can't make an association between the two (which would get rid of the need for the forEach). Is that true?
Second:
If the loop is the right way to do this, how could I return the results from the forEach into the next .then and send it using res.send()?
javascript node.js express foreach sequelize.js
In the first.then()
from where isresponse
coming from ?
– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
1
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40
add a comment |
I have a somewhat complicated sequelize query running in an express route, and I have a need to run a second query afterwards, to append needed data to the returned result:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF' },
include: [
{ model: models.form, as: 'form' },
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
response.forEach( item ) => {
models.employees.findAll({
attributes: [ 'fname', 'mname', 'lname' ],
where: { empl_id: item.form[0].empl_id },
order: [ 'empl_id' ]
}).then(function(response){
item.readableName = response;
});
});
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
The issue I'm having here (with the current setup) is that I need the forEach to finish appending data before I call res.send(response). If I take the last .then() off of this code and put res.send in the same block as the loop, the rest of the data gets sent correctly but no results from the employees table show. However, with the way it is set up above, nothing is sent to the response object that I am trying to send.
My question is twofold
First:
The employees table doesn't have any columns that match the original table (fas). Because of that, it's my understanding that I can't make an association between the two (which would get rid of the need for the forEach). Is that true?
Second:
If the loop is the right way to do this, how could I return the results from the forEach into the next .then and send it using res.send()?
javascript node.js express foreach sequelize.js
I have a somewhat complicated sequelize query running in an express route, and I have a need to run a second query afterwards, to append needed data to the returned result:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF' },
include: [
{ model: models.form, as: 'form' },
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
response.forEach( item ) => {
models.employees.findAll({
attributes: [ 'fname', 'mname', 'lname' ],
where: { empl_id: item.form[0].empl_id },
order: [ 'empl_id' ]
}).then(function(response){
item.readableName = response;
});
});
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
The issue I'm having here (with the current setup) is that I need the forEach to finish appending data before I call res.send(response). If I take the last .then() off of this code and put res.send in the same block as the loop, the rest of the data gets sent correctly but no results from the employees table show. However, with the way it is set up above, nothing is sent to the response object that I am trying to send.
My question is twofold
First:
The employees table doesn't have any columns that match the original table (fas). Because of that, it's my understanding that I can't make an association between the two (which would get rid of the need for the forEach). Is that true?
Second:
If the loop is the right way to do this, how could I return the results from the forEach into the next .then and send it using res.send()?
javascript node.js express foreach sequelize.js
javascript node.js express foreach sequelize.js
asked Nov 21 '18 at 17:56
Jensen010Jensen010
11611
11611
In the first.then()
from where isresponse
coming from ?
– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
1
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40
add a comment |
In the first.then()
from where isresponse
coming from ?
– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
1
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40
In the first
.then()
from where is response
coming from ?– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
In the first
.then()
from where is response
coming from ?– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
1
1
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40
add a comment |
1 Answer
1
active
oldest
votes
Well, as usual I thought of a solution shortly after posting this. Turns out you can use nested includes in Sequelize, and I have an association between the employees table and another one I was already using. The solution looks like this:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.hrtf_fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF'
},
include: [
{ model: models.form, as: 'form',
include: [{
model: models.employees, as: 'employees',
attributes: [ 'fname', 'lname' ]
}]
},
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
I need to do some concats, but I think this will do :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417995%2fsending-sequelize-results-through-express-after-foreach-loop-has-run%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, as usual I thought of a solution shortly after posting this. Turns out you can use nested includes in Sequelize, and I have an association between the employees table and another one I was already using. The solution looks like this:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.hrtf_fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF'
},
include: [
{ model: models.form, as: 'form',
include: [{
model: models.employees, as: 'employees',
attributes: [ 'fname', 'lname' ]
}]
},
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
I need to do some concats, but I think this will do :)
add a comment |
Well, as usual I thought of a solution shortly after posting this. Turns out you can use nested includes in Sequelize, and I have an association between the employees table and another one I was already using. The solution looks like this:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.hrtf_fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF'
},
include: [
{ model: models.form, as: 'form',
include: [{
model: models.employees, as: 'employees',
attributes: [ 'fname', 'lname' ]
}]
},
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
I need to do some concats, but I think this will do :)
add a comment |
Well, as usual I thought of a solution shortly after posting this. Turns out you can use nested includes in Sequelize, and I have an association between the employees table and another one I was already using. The solution looks like this:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.hrtf_fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF'
},
include: [
{ model: models.form, as: 'form',
include: [{
model: models.employees, as: 'employees',
attributes: [ 'fname', 'lname' ]
}]
},
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
I need to do some concats, but I think this will do :)
Well, as usual I thought of a solution shortly after posting this. Turns out you can use nested includes in Sequelize, and I have an association between the employees table and another one I was already using. The solution looks like this:
//Get inboxes based on passed params
router.get('/:data', function(req, res, next) {
let passedData = JSON.parse(req.params.data);
models.hrtf_fas.findAll({
where: { current_stop: { $like: '%'+passedData.current_stop +'%'},
cert_class: { $like: '%'+passedData.cert_class +'%'},
fis_yr: { $like: '%'+passedData.fis_yr +'%'},
work_loc: { $like: '%'+passedData.work_loc +'%'},
action_cd: { $like: '%'+passedData.action_cd +'%'},
form_num: { $like: '%'+passedData.form_num +'%'},
form_id: 'HRTF'
},
include: [
{ model: models.form, as: 'form',
include: [{
model: models.employees, as: 'employees',
attributes: [ 'fname', 'lname' ]
}]
},
{ model: models.form_status, as: 'form_status' },
{ model: models.action_codes, as: 'action_codes' }
],
order: ['form_num']
}).then(function(response){
res.send(response);
}).catch(function(error) {
return next(error);
});
});
I need to do some concats, but I think this will do :)
answered Nov 21 '18 at 18:39
Jensen010Jensen010
11611
11611
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417995%2fsending-sequelize-results-through-express-after-foreach-loop-has-run%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In the first
.then()
from where isresponse
coming from ?– Anirudh Mangalvedhekar
Nov 21 '18 at 18:38
1
models.fas.findAll({
– Jensen010
Nov 21 '18 at 18:40