How to get an answer that is in the request header

Asked

Viewed 797 times

0

I’m making a site that contains a forum and I need to pick up the amount of topics and registered users and with this in my back-end I send these quantities by header. I’m using in my back-end Cors, Express, Knex, Nodemailer, Sqlite3.

async listAll(request, response){
    const { page = 1 } = request.query;
    const [count_topics] = await connection('topics')
    .count();
    const [count_users] = await connection('users')
    .count();
    const topics = await connection('topics')
    .join('users', 'users.id_user', '=', 'topics.user_id')
    .orderBy('id_topic', 'desc')
    .limit(3)
    .offset((page - 1) * 3)
    .select(['topics.id_topic', 'topics.title', 'topics.description', 'topics.date_topic', 'users.user']);
    const recent_topics = await connection('topics')
    .join('users', 'users.id_user', '=', 'topics.user_id')
    .orderBy('id_topic', 'desc')
    .limit(7)
    .offset((page - 1) * 7)
    .select(['topics.id_topic', 'topics.title', 'topics.date_topic', 'users.user']);
    response.header('X-Total-Count-Topics', count_topics['count(*)']); //aqui estou enviando a quantidade de tópicos pelo header
    response.header('X-Total-Count-Users', count_users['count(*)']); //aqui estou enviando a quantidade de usuarios cadastrados pelo header
    const send = {topics, recent_topics};
    return response.json(send);
}

And this information is in the header as I entered.

inserir a descrição da imagem aqui

Here is my front end done in React where I try to get the information that is in the header. I am using in my front-end Axios, html-React-parser, React-router-dom, React-scripts, sweetalert2.

const [topics, setTopics] = useState([]);
const [recentTopics, setRecentTopics] = useState([]);
const [totalTopics, setTotalTopics] = useState(0);
const [totalUsers, setTotalUsers] = useState(0);
async function loadTopics() {
    const response = await api.get('/topics');
    setTopics(response.data.topics);
    setRecentTopics(response.data.recent_topics);
    setTotalTopics(response.headers['X-Total-Count-Topics']); //eu tentei pegar desse jeito mas aparece undefined
    setTotalUsers(response.headers['X-Total-Count-Users']); //eu tentei pegar desse jeito mas aparece undefined
}
useEffect(() => {
    loadTopics();
}, []);

When when I use what I’ve stored in the state basically does not show the amount of users and topics that are in the header, I’ve already put a console.log() and it appears Undefined.

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

I wanted to know how I get this data I sent to my header so I can continue on my project.

  • I’ll take a look later, but can’t you pass that information on to the backend json and retrieve it on the front? if I can and I don’t know how to do it, just say I’m trying to make a simple example in code and I’ll show you

  • gives a console.log(Response.headers) and console.log(Response.data) and sees what appears if there is this information there you are trying to access the wrong way, and says what is appearing in Preview and Response

1 answer

0

As for the headers leaves so:

setTotalTopics(response.headers['x-total-count-topics']);
setTotalUsers(response.headers['x-total-count-users']); 

As for the other data read my comment and post, we need more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.