JOIN the table in each element in NodeJS












0















This is what my controller currently looks like, which returns the following result:



exports.getSenderdata = (req, res) => {
const userId = req.params.senderId;
Transaction.findAll({
where: {
id_sender: userId,
},
attributes: [
'amount_money',
'date_time',
'transfer_title',
'id_recipient',
'id_sender',
],
}).then(transactions => {
res.send(transactions);
});
};


// results:



 [
{
"amount_money": 5,
"date_time": "2019-01-19T20:15:07.000Z",
"transfer_title": "test",
"id_recipient": 2,
"id_sender": 1
},
{
"amount_money": 2,
"date_time": "2019-01-19T20:20:10.000Z",
"transfer_title": "payment example",
"id_recipient": 2,
"id_sender": 1
}
]


I would like to include a different table to each element in json, that id_recipient on this results = id in different table. so I would like this result > Is this something possible?



[
{
"amount_money": 5,
"date_time": "2019-01-19T20:15:07.000Z",
"transfer_title": "test",
"id_recipient": 2,
"id_sender": 1
"user": {
"name": "Claudie"
}
},
{
"amount_money": 2,
"date_time": "2019-01-19T20:20:10.000Z",
"transfer_title": "payment example",
"id_recipient": 2,
"id_sender": 1
"user": {
"name": "John"
}
}
]


I have a solution that I need: I want to send this string, I do not know how.



    exports.getRecipientdata = (req, res) => {
const userId = req.params.recipientId;
const sendersArray = ;
Transaction.findAll({
where: {
id_sender: userId,
},
})
.then(transactions =>
Promise.all(
transactions.map(({ id_recipient }) =>
User.findAll({
where: {
id: id_recipient,
},
attributes: ['name', 'surname'],
include: [
{
model: Transaction,
where: { id_sender: db.Sequelize.col('user.id') },
attributes: [
'amount_money',
'date_time',
'transfer_title',
'id_recipient',
'id_sender',
],
},
],
})
.then(sender => {
console.log(JSON.stringify(sender)); // <- this is string, i need this to send res
})
.catch(err => {
console.log(err);
}),
),
),
)
.then(() => {
// ?
});
};









share|improve this question





























    0















    This is what my controller currently looks like, which returns the following result:



    exports.getSenderdata = (req, res) => {
    const userId = req.params.senderId;
    Transaction.findAll({
    where: {
    id_sender: userId,
    },
    attributes: [
    'amount_money',
    'date_time',
    'transfer_title',
    'id_recipient',
    'id_sender',
    ],
    }).then(transactions => {
    res.send(transactions);
    });
    };


    // results:



     [
    {
    "amount_money": 5,
    "date_time": "2019-01-19T20:15:07.000Z",
    "transfer_title": "test",
    "id_recipient": 2,
    "id_sender": 1
    },
    {
    "amount_money": 2,
    "date_time": "2019-01-19T20:20:10.000Z",
    "transfer_title": "payment example",
    "id_recipient": 2,
    "id_sender": 1
    }
    ]


    I would like to include a different table to each element in json, that id_recipient on this results = id in different table. so I would like this result > Is this something possible?



    [
    {
    "amount_money": 5,
    "date_time": "2019-01-19T20:15:07.000Z",
    "transfer_title": "test",
    "id_recipient": 2,
    "id_sender": 1
    "user": {
    "name": "Claudie"
    }
    },
    {
    "amount_money": 2,
    "date_time": "2019-01-19T20:20:10.000Z",
    "transfer_title": "payment example",
    "id_recipient": 2,
    "id_sender": 1
    "user": {
    "name": "John"
    }
    }
    ]


    I have a solution that I need: I want to send this string, I do not know how.



        exports.getRecipientdata = (req, res) => {
    const userId = req.params.recipientId;
    const sendersArray = ;
    Transaction.findAll({
    where: {
    id_sender: userId,
    },
    })
    .then(transactions =>
    Promise.all(
    transactions.map(({ id_recipient }) =>
    User.findAll({
    where: {
    id: id_recipient,
    },
    attributes: ['name', 'surname'],
    include: [
    {
    model: Transaction,
    where: { id_sender: db.Sequelize.col('user.id') },
    attributes: [
    'amount_money',
    'date_time',
    'transfer_title',
    'id_recipient',
    'id_sender',
    ],
    },
    ],
    })
    .then(sender => {
    console.log(JSON.stringify(sender)); // <- this is string, i need this to send res
    })
    .catch(err => {
    console.log(err);
    }),
    ),
    ),
    )
    .then(() => {
    // ?
    });
    };









    share|improve this question



























      0












      0








      0








      This is what my controller currently looks like, which returns the following result:



      exports.getSenderdata = (req, res) => {
      const userId = req.params.senderId;
      Transaction.findAll({
      where: {
      id_sender: userId,
      },
      attributes: [
      'amount_money',
      'date_time',
      'transfer_title',
      'id_recipient',
      'id_sender',
      ],
      }).then(transactions => {
      res.send(transactions);
      });
      };


      // results:



       [
      {
      "amount_money": 5,
      "date_time": "2019-01-19T20:15:07.000Z",
      "transfer_title": "test",
      "id_recipient": 2,
      "id_sender": 1
      },
      {
      "amount_money": 2,
      "date_time": "2019-01-19T20:20:10.000Z",
      "transfer_title": "payment example",
      "id_recipient": 2,
      "id_sender": 1
      }
      ]


      I would like to include a different table to each element in json, that id_recipient on this results = id in different table. so I would like this result > Is this something possible?



      [
      {
      "amount_money": 5,
      "date_time": "2019-01-19T20:15:07.000Z",
      "transfer_title": "test",
      "id_recipient": 2,
      "id_sender": 1
      "user": {
      "name": "Claudie"
      }
      },
      {
      "amount_money": 2,
      "date_time": "2019-01-19T20:20:10.000Z",
      "transfer_title": "payment example",
      "id_recipient": 2,
      "id_sender": 1
      "user": {
      "name": "John"
      }
      }
      ]


      I have a solution that I need: I want to send this string, I do not know how.



          exports.getRecipientdata = (req, res) => {
      const userId = req.params.recipientId;
      const sendersArray = ;
      Transaction.findAll({
      where: {
      id_sender: userId,
      },
      })
      .then(transactions =>
      Promise.all(
      transactions.map(({ id_recipient }) =>
      User.findAll({
      where: {
      id: id_recipient,
      },
      attributes: ['name', 'surname'],
      include: [
      {
      model: Transaction,
      where: { id_sender: db.Sequelize.col('user.id') },
      attributes: [
      'amount_money',
      'date_time',
      'transfer_title',
      'id_recipient',
      'id_sender',
      ],
      },
      ],
      })
      .then(sender => {
      console.log(JSON.stringify(sender)); // <- this is string, i need this to send res
      })
      .catch(err => {
      console.log(err);
      }),
      ),
      ),
      )
      .then(() => {
      // ?
      });
      };









      share|improve this question
















      This is what my controller currently looks like, which returns the following result:



      exports.getSenderdata = (req, res) => {
      const userId = req.params.senderId;
      Transaction.findAll({
      where: {
      id_sender: userId,
      },
      attributes: [
      'amount_money',
      'date_time',
      'transfer_title',
      'id_recipient',
      'id_sender',
      ],
      }).then(transactions => {
      res.send(transactions);
      });
      };


      // results:



       [
      {
      "amount_money": 5,
      "date_time": "2019-01-19T20:15:07.000Z",
      "transfer_title": "test",
      "id_recipient": 2,
      "id_sender": 1
      },
      {
      "amount_money": 2,
      "date_time": "2019-01-19T20:20:10.000Z",
      "transfer_title": "payment example",
      "id_recipient": 2,
      "id_sender": 1
      }
      ]


      I would like to include a different table to each element in json, that id_recipient on this results = id in different table. so I would like this result > Is this something possible?



      [
      {
      "amount_money": 5,
      "date_time": "2019-01-19T20:15:07.000Z",
      "transfer_title": "test",
      "id_recipient": 2,
      "id_sender": 1
      "user": {
      "name": "Claudie"
      }
      },
      {
      "amount_money": 2,
      "date_time": "2019-01-19T20:20:10.000Z",
      "transfer_title": "payment example",
      "id_recipient": 2,
      "id_sender": 1
      "user": {
      "name": "John"
      }
      }
      ]


      I have a solution that I need: I want to send this string, I do not know how.



          exports.getRecipientdata = (req, res) => {
      const userId = req.params.recipientId;
      const sendersArray = ;
      Transaction.findAll({
      where: {
      id_sender: userId,
      },
      })
      .then(transactions =>
      Promise.all(
      transactions.map(({ id_recipient }) =>
      User.findAll({
      where: {
      id: id_recipient,
      },
      attributes: ['name', 'surname'],
      include: [
      {
      model: Transaction,
      where: { id_sender: db.Sequelize.col('user.id') },
      attributes: [
      'amount_money',
      'date_time',
      'transfer_title',
      'id_recipient',
      'id_sender',
      ],
      },
      ],
      })
      .then(sender => {
      console.log(JSON.stringify(sender)); // <- this is string, i need this to send res
      })
      .catch(err => {
      console.log(err);
      }),
      ),
      ),
      )
      .then(() => {
      // ?
      });
      };






      javascript node.js sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 10:32







      ReactRouter4

















      asked Jan 20 at 9:59









      ReactRouter4ReactRouter4

      537




      537
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Instead of:



              .then(sender => {
          sendersArray.push(sender);
          })


          Try:



              .then(sender => {
          sendersArray.push(sender);
          if (sendersArray.length == transactions.length)
          res.send(sendersArray);
          })


          and remove the res.send(sendersArray); at the end.



          Promise.all should work something like this:



          .then(transactions =>
          Promise.all(
          transactions.map(({ id_recipient }) => { return new Promise( (resolve, reject) => {
          User.findAll({
          where: {
          id: id_recipient,
          },
          attributes: ['name', 'surname'],
          include: [
          {
          model: Transaction,
          where: { id_sender: db.Sequelize.col('user.id') },
          attributes: [
          'amount_money',
          'date_time',
          'transfer_title',
          'id_recipient',
          'id_sender',
          ],
          },
          ],
          })
          .then(sender => {
          resolve(JSON.stringify(sender)); // <- this is string, i need this to send res
          })
          .catch(err => {
          reject(err);
          }),
          })}),
          ),
          )
          .then((results) => {
          res.send(results);
          });





          share|improve this answer


























          • Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

            – ReactRouter4
            Jan 20 at 10:21













          • post your code @ReactRouter4

            – d9ngle
            Jan 20 at 10:28











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275314%2fjoin-the-table-in-each-element-in-nodejs%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









          1














          Instead of:



              .then(sender => {
          sendersArray.push(sender);
          })


          Try:



              .then(sender => {
          sendersArray.push(sender);
          if (sendersArray.length == transactions.length)
          res.send(sendersArray);
          })


          and remove the res.send(sendersArray); at the end.



          Promise.all should work something like this:



          .then(transactions =>
          Promise.all(
          transactions.map(({ id_recipient }) => { return new Promise( (resolve, reject) => {
          User.findAll({
          where: {
          id: id_recipient,
          },
          attributes: ['name', 'surname'],
          include: [
          {
          model: Transaction,
          where: { id_sender: db.Sequelize.col('user.id') },
          attributes: [
          'amount_money',
          'date_time',
          'transfer_title',
          'id_recipient',
          'id_sender',
          ],
          },
          ],
          })
          .then(sender => {
          resolve(JSON.stringify(sender)); // <- this is string, i need this to send res
          })
          .catch(err => {
          reject(err);
          }),
          })}),
          ),
          )
          .then((results) => {
          res.send(results);
          });





          share|improve this answer


























          • Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

            – ReactRouter4
            Jan 20 at 10:21













          • post your code @ReactRouter4

            – d9ngle
            Jan 20 at 10:28
















          1














          Instead of:



              .then(sender => {
          sendersArray.push(sender);
          })


          Try:



              .then(sender => {
          sendersArray.push(sender);
          if (sendersArray.length == transactions.length)
          res.send(sendersArray);
          })


          and remove the res.send(sendersArray); at the end.



          Promise.all should work something like this:



          .then(transactions =>
          Promise.all(
          transactions.map(({ id_recipient }) => { return new Promise( (resolve, reject) => {
          User.findAll({
          where: {
          id: id_recipient,
          },
          attributes: ['name', 'surname'],
          include: [
          {
          model: Transaction,
          where: { id_sender: db.Sequelize.col('user.id') },
          attributes: [
          'amount_money',
          'date_time',
          'transfer_title',
          'id_recipient',
          'id_sender',
          ],
          },
          ],
          })
          .then(sender => {
          resolve(JSON.stringify(sender)); // <- this is string, i need this to send res
          })
          .catch(err => {
          reject(err);
          }),
          })}),
          ),
          )
          .then((results) => {
          res.send(results);
          });





          share|improve this answer


























          • Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

            – ReactRouter4
            Jan 20 at 10:21













          • post your code @ReactRouter4

            – d9ngle
            Jan 20 at 10:28














          1












          1








          1







          Instead of:



              .then(sender => {
          sendersArray.push(sender);
          })


          Try:



              .then(sender => {
          sendersArray.push(sender);
          if (sendersArray.length == transactions.length)
          res.send(sendersArray);
          })


          and remove the res.send(sendersArray); at the end.



          Promise.all should work something like this:



          .then(transactions =>
          Promise.all(
          transactions.map(({ id_recipient }) => { return new Promise( (resolve, reject) => {
          User.findAll({
          where: {
          id: id_recipient,
          },
          attributes: ['name', 'surname'],
          include: [
          {
          model: Transaction,
          where: { id_sender: db.Sequelize.col('user.id') },
          attributes: [
          'amount_money',
          'date_time',
          'transfer_title',
          'id_recipient',
          'id_sender',
          ],
          },
          ],
          })
          .then(sender => {
          resolve(JSON.stringify(sender)); // <- this is string, i need this to send res
          })
          .catch(err => {
          reject(err);
          }),
          })}),
          ),
          )
          .then((results) => {
          res.send(results);
          });





          share|improve this answer















          Instead of:



              .then(sender => {
          sendersArray.push(sender);
          })


          Try:



              .then(sender => {
          sendersArray.push(sender);
          if (sendersArray.length == transactions.length)
          res.send(sendersArray);
          })


          and remove the res.send(sendersArray); at the end.



          Promise.all should work something like this:



          .then(transactions =>
          Promise.all(
          transactions.map(({ id_recipient }) => { return new Promise( (resolve, reject) => {
          User.findAll({
          where: {
          id: id_recipient,
          },
          attributes: ['name', 'surname'],
          include: [
          {
          model: Transaction,
          where: { id_sender: db.Sequelize.col('user.id') },
          attributes: [
          'amount_money',
          'date_time',
          'transfer_title',
          'id_recipient',
          'id_sender',
          ],
          },
          ],
          })
          .then(sender => {
          resolve(JSON.stringify(sender)); // <- this is string, i need this to send res
          })
          .catch(err => {
          reject(err);
          }),
          })}),
          ),
          )
          .then((results) => {
          res.send(results);
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 10:58

























          answered Jan 20 at 10:08









          d9ngled9ngle

          5251418




          5251418













          • Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

            – ReactRouter4
            Jan 20 at 10:21













          • post your code @ReactRouter4

            – d9ngle
            Jan 20 at 10:28



















          • Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

            – ReactRouter4
            Jan 20 at 10:21













          • post your code @ReactRouter4

            – d9ngle
            Jan 20 at 10:28

















          Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

          – ReactRouter4
          Jan 20 at 10:21







          Yes, it's working. I can use a promise too, but I have a problem with results, because I return double the same select. why?

          – ReactRouter4
          Jan 20 at 10:21















          post your code @ReactRouter4

          – d9ngle
          Jan 20 at 10:28





          post your code @ReactRouter4

          – d9ngle
          Jan 20 at 10:28




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275314%2fjoin-the-table-in-each-element-in-nodejs%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre