简体   繁体   English

如何在 node.js 中的 Post api 中调用 POST api,快递服务器

[英]How to call a POST api inside a Post api in node.js, express server

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.我正在尝试使用后端 node.js api url 在 react.js 中进行提取,然后使用另一个 url 在服务器内进一步调用 api 到另一条路由。

How am i supposed to do that?我该怎么做? Take a look at the code below:看看下面的代码:

From the frontend "/confirm" api will be called using fetch.从前端“/confirm”api 将使用 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还是不行

You don't need to call your route again.您无需再次调用您的路线。 Just create an function and call it.只需创建一个 function 并调用它。


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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM