Getting the name of parameters from Vue 3/Express

Greetings, I want to start by apologizing if the title of my question caused any confusion for those reading it. I struggled to find the right wording. The issue at hand involves a problem with sending a get request from my axios instance to my express instance. When I send data from axios, the object displays all the necessary data. However, when I attempt to use that data in express using req.params.email, the value it holds is :email. Here is the code snippet:

Client/Axios

  checkEmail (data) {
    return http.get('/users/:email', data)
  },

Value of 'data' Object

{email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47132f2e340e34070a2a262e2b69083520">[email protected]</a>'}

Server/Express

  router.get('/users/:email', function (req, res) {
    console.log(req.params.email)
    const data = User.find({ email: req.params.email })
    return res.sendStatus(200).json(data)
  })

Server Console Log

{ email: ':email' }

On a related note, I have attempted to just use req.params but it yields the same result. I would greatly appreciate any insights on what might be going wrong!

Answer №1

This code is effective when switching from hard-coded data to using dynamic data retrieval.

Replace:

    const data = {
        email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df9e8fef9cdeae0ece4e1a3eee2e0">[email protected]</a>',
        first_name: 'Tom',
        last_name: 'Cruise'
    }

With:

    const data = User.find({ email: req.params.email  })

server.js

const express = require("express")
const axios = require('axios')
const cors = require("cors")

const app = express()
app.use(cors())

app.get("/users/:email", async (req, res) => {
    console.log("params", req.params);
    // const data = User.find({ email: req.params.email  })
    const data = {
        email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="473322343307202a262e2b6924282a">[email protected]</a>',
        first_name: 'Tom',
        last_name: 'Cruise'
    }
    res.status(200).json(data);
});

app.listen(3000, () => { console.log("Listening on :3000") })

client.js

const axios = require('axios')
const checkEmail = async (data) => {
    try {
        const response = await axios.get(`http://localhost:3000/users/${data}`);
        return Promise.resolve(response.data)
    } catch (error) {
        return Promise.reject(error)
    }
};

checkEmail('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07536f6e744e74474a7e426a66666629487560">[email protected]</a>')
    .then(result => {
        console.log('user is : ' + JSON.stringify(result))
    })
    .catch(error => {
        console.log(error.message)
    });

Install dependencies

npm install express axios cors

Run server

node server.js

Run client

node client.js

Server Result:

https://i.stack.imgur.com/R97ia.png

Client Result:

https://i.stack.imgur.com/6XWqS.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering problems while trying to run a discord.js v14.3.0 bot on a virtual private server

I just set up a VPS to host my discord.js bot, but I keep encountering errors when checking the logs from PM2 [pm2 log], causing the bot to go offline and encounter errors. I'm puzzled because the template was originally used in a repl.it project whe ...

Is it possible to simultaneously run both an npm server and a proxy in separate directories with just one command in

We are currently working on a project that involves a 'server' folder and a 'client' folder. In order to run the project, we have to navigate to the 'server' folder, enter 'npm run nodemon' in the terminal, and then ...

Removing a post through the integration of socket.io, MySQL, Node.js, and Express

Currently, I am working on creating a basic messaging app that enables users to add statuses and update them in real-time for everyone to see. The technologies I am using include socket.io, MySQL, Node.js, and Express. Users have the ability to send messa ...

Encountered an uncaughtException in Node.js and mongoDB: User model cannot be overwritten once compiled

Currently, I am utilizing this import statement const User = require("./Usermodel")' However, I would like to modify it to const User = require("./UserModel") Despite my efforts to align the spelling of the import with my other i ...

When using express, the response.sendFile() function is causing an Uncaught SyntaxError with the error message: Unexpected token <

Currently, I'm utilizing react-router with browserHistory. There is a specific function in my code that is meant to send the Index.html file since the routing is handled client-side with react-router. However, there seems to be an issue where the serv ...

Vue Server-Side Rendering does not retain the URL hash during redirection

Having an issue with Vue.js application and Server-side Rendering (SSR). For example, I have a page with URL anchors like this http://localhost/faq#question5 However, when running SSR, the hash part of the URL is lost. According to the documentation: r ...

Error: The function Object.entries is not defined

Why does this error persist every time I attempt to start my Node.js/Express server? Does this issue relate to the latest ES7 standards? What requirements must be met in order to run an application utilizing these advanced functionalities? ...

Passport appears to be experiencing amnesia when it comes to remembering the user

After extensive research online, I have yet to find a solution to my issue. Therefore, I am reaching out here for assistance. I am currently working on implementing sessions with Passport. The registration and login functionalities are functioning properl ...

Error in Next.js using next-auth/react: "PrismaClient cannot be executed in the browser"

Currently, I am in the process of developing a Next.js application and implementing authentication using the next-auth/react package. One of the server-side functions I have created utilizes PrismaClient to retrieve information about the current user based ...

Building a secure NodeJS REST API with authentication capabilities utilizing Passport and OAuth2 with the integration of

Currently, I am in the process of developing a RESTful API using NodeJS and for authentication purposes, I have opted to utilize Passport. In order to maintain true RESTfulness, I have decided to implement token-based authentication instead of sessions. M ...

Step-by-step guide on deploying an Angular and Express project to Google App Engine

I am in the process of deploying my app to Google App Engine. I have already set up App Engine and installed gcloud on my local machine. While I have been successful in deploying some projects, I have only done so for Angular applications. My goal now is ...

I've been attempting to develop a React application, but I consistently encounter the following error: "npm ERR! cb() function was never invoked!"

Here is the issue at hand: HP@DESKTOP-1HP83V8 MINGW64 ~/Desktop/Web-Development (master) $ npx create-react-app my-app A new React app is being created in C:\Users\HP\Desktop\Web-Development\my-app. Packages are being installed. ...

The dependency that was installed in the node_modules directory is now showing as missing the

I have encountered an issue with 2 TS packages. The first package, project-1, is installed as a dependency in the second package, project-2. While I am able to import and access all type definitions of project-1 in project-2, the dependencies (node_modules ...

Extract a value from a json document

Hey there! I'm looking to create an unwhitelist command. When a user is mentioned or their ID is provided, I want it to be removed from the JSON file without checking if the ID exists. Do you have any suggestions on how to accomplish this? Here is my ...

Struggling with retrieving empty parameters, queries, and body from an Axios API call for the past three days. It's a learning experience as I am new to the MERN stack

There seems to be an issue where only empty JSON like {} is coming as output. I am looking for a way to access the parameters sent from the routes in this scenario. On the Frontend: let api = axios.create({ baseURL: "http://localhost:5000/validateSignI ...

What is the best method for directing a search URL with an embedded query string?

Currently, I am developing an express application that has two different GET URLs. The first URL retrieves all resources from the database but is protected by authentication and requires admin access. The second URL fetches resources based on a search para ...

A guide on converting TypeScript to JavaScript while utilizing top-level await

Exploring the capabilities of top-level await introduced with TypeScript 3.8 in a NodeJS setting. Here's an example of TypeScript code utilizing this feature: import { getDoctorsPage } from "./utils/axios.provider"; const page = await getDo ...

Utilizing unique layouts for specific views in sails.js and angular.js

I am currently working on a Sails.js + Angular.js project with a single layout. However, I have come across different HTML files that have a completely different layout compared to the one I am using. The layout file I am currently using is the default la ...

Accessing files from various directories within my project

I'm working on a project with 2 sources and I need to import a file from MyProject into nest-project-payment. Can you please guide me on how to do this? Here is the current file structure of my project: https://i.stack.imgur.com/KGKnp.png I attempt ...

In the realm of JavaScript, the localeCompare() string method is more than willing to accept the presence of 'undefined' as a valid parameter for 'locale', while opting to outright reject any instance of

I have discovered a method to sort strings naturally const rows = ['37-SK', '4-ML', '41-NP', '2-YZ', '21', '26-BF']; console.log(rows.sort((a, b) => a.localeCompare(b, undefined, { numeric: tru ...