1

I am trying to make a fetch in react.js using backend node.js api url which then further makes a post api call within the server to another route using another url.

How am i supposed to do that? Take a look at the code below:

From the frontend "/confirm" api will be called using fetch.

app.post("/save-info",(req,res)=>{
   //Does some more stuff and returns a 
   response to the confirm api.

}

app.post("/confirm", (req,res)=>{
   //Does some stuff

   //Makes another call inside this api 
   to the "/save-info" route

}

Updated Query

Guys, please take a look at the code below

async function signUp(info) {
    const {
        firstName,
        lastName,
        address,
        email,
        phoneNumber,
        password,
        city,
        postal_code,
    } = info;

    console.log("only info: ", phoneNumber);

    const queryInsertNewUser = `INSERT INTO public."Users"(
        "First_Name", "Email", "Mobile", "Address", "User_Type", "Last_Name", "password", "city","postal_code")
        VALUES ('${firstName}', '${email}', '${phoneNumber}', '${address}', 'Customer', '${lastName}', '${password}','${city}','${postal_code}')
        RETURNING user_id;`;

    // return { email: "kalo", id: "23" };

    client.query(queryInsertNewUser, (err, result) => {
        if (!err) {
            if (result.rowCount == 1) {
                console.log("User registered.");
                return {
                    status: "Success",
                    msg: "User Registered Successfully",
                    user_id: result.rows[0].user_id,
                };
            } else {
                console.log("Not Registered.");
                return {
                    status: "Error",
                    msg: "Could not register user. Call Developer.",
                };
            }
        } else {
            console.log(err);
        }
    });
}


app.post("/signup", async(req, res) => {
    const { email } = req.body;
    const data = await signUp(req.body);
    console.log(data);
});

data is printing undefined. Still it does not work

Hasin Sadique
  • 17
  • 1
  • 6

1 Answers1

0

You don't need to call your route again. Just create an function and call it.


const saveInfo = ()=>{
  // do wathever you want here
  return "some stuff done"
}

app.post("/save-info",(req,res)=>{
   // you probabbly don't need this route.

}

app.post("/confirm", (req,res)=>{
   //Does some stuff

   const data = saveInfo()
   return res.send({success:true, done})

}

  • Tried your way but saveInfo function is returning undefined. Please Note that, inside saveInfo function I am trying to insert values in the postgresql DB which returns the id of the inserted field. – Hasin Sadique Jan 09 '23 at 17:51
  • 1
    @HasinSadique In your case, calling a function is the right way as mentioned in this answer. Show through your code whatever error you are facing after implementing this aforementioned approach. – Daniyal Malik Jan 10 '23 at 06:58
  • @DaniyalMalik Please take a look at the code below updated query. – Hasin Sadique Jan 10 '23 at 17:29
  • @HasinSadique You are using callback to handle the result from `client.query`, use `await` instead. I will update my answer as well. – Daniyal Malik Jan 11 '23 at 10:41
  • @HasinSadique In short, just replace your current `client.query` call with `const res = await client.query(queryInsertNewUser);` – Daniyal Malik Jan 11 '23 at 11:01