A
A
Alex102142021-05-28 10:46:41
Node.js
Alex10214, 2021-05-28 10:46:41

How to correctly request data at the time of ngOnInit?

Greetings, I have such a problem ... At the time of page initialization, I have requests to the server. I am using express and sequelize. I looked at the courses and everything seems to be ok. But they didn't explain how to make multiple queries. Here is my code:
This is a router in which the getArrSelectDataType, getArrSelectTicker, getArrSelectSource functions are called.
Everything is ok. In this case, I have one get function and two post methods.

const express = require('express');
const controller = require('../controllers/data_table')
const router = express.Router();

router.get('/', controller.getArrSelectDataType);
router.post('/', controller.getArrSelectTicker);
router.post('/', controller.getArrSelectSource);

module.exports = router;


Next, I have the functions themselves ..
const Data_table = require('../models/Data_table');
console.log('11ssssssssss')

module.exports.getArrSelectDataType = async (req, res) => {
  console.log('dddddddddddd')

  const group_name = await Data_table.findAll({
    attributes: ['group_name'],
    group: 'group_name'
  })
  const arrSimple = group_name.map(el => {
    return el.group_name
  });

  res.json({
    arrSimple
  })
}

module.exports.getArrSelectTicker = async (req, res) => {
  console.log('hhhhhhhhhhh')

  const value = await Data_table.findAll({
    attributes: ['value'],
    group: 'value'
  });
   const arrValue = value.map(el => {
     return el.value
   });
  res.json({
    arrValue
  })
}

module.exports.getArrSelectSource = async (req, res) => {
  console.log('eeeeeeeeeee')
  const source = await  Data_table.findAll({
    attributes: ['source'],
    group: 'source'
  })
  const arrSource = source.map(el => {
    return el
  });

  res.json({
    arrSource
  })
}

in this scenario, the getArrSelectDataType function (by the get method) works for me and the getArrSelectTicker function (by the post method) works TWO TIMES, and the following getArrSelectSource function does not work at all. And I understood this because the previous function worked for me twice. I understand that this may be due to the POST method. But I don't understand how to call both functions. What do I need to do so that the third function ( getArrSelectSource ) could be called again by the button. Please tell me I really need help..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2021-05-28
@Alex10214

Your routes are the same:

router.get('/', controller.getArrSelectDataType);
router.post('/', controller.getArrSelectTicker);
router.post('/', controller.getArrSelectSource);

How is the node supposed to figure out where to post for the getArrSelectTicker controller or post for the getArrSelectSource controller?
Write the routes correctly, for example:
router.get('/get-arr-select-data-type', controller.getArrSelectDataType);
router.post('/get-arr-select-data-ticker', controller.getArrSelectTicker);
router.post('/get-arr-select-data-source', controller.getArrSelectSource);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question