簡體   English   中英

如何使用 node.js express 在另一個 app.post 中使用 app.post 中的變量?

[英]How can I use variables from app.post in another app.post using node js express?

我正在嘗試使用我在 app.post 中定義為 const 的變量,因此當用戶在表單中提交輸入時,我將接收並存儲它。 但是當我嘗試再次將變量發送給用戶以便讓用戶檢查 app.post 中的完整信息時,它顯示變量未定義。

我在這里錯過了什么? 請在下面查看我的代碼

app.get('/',function(request,response){
  response.sendFile(__dirname+"/CreatingService.html")
  })
});

  app.post('/CreatingService', (req, res) =>{

    const RecID = req.body.arg1;
    const assetno = req.body.arg2;
    const space = req.body.arg3;
    const pdate = req.body.arg4;
    const dist = req.body.arg5;
    const state = req.body.arg6;
    const status = req.body.arg7;
    const first = req.body.arg8;
    const middle = req.body.arg9;
    const last = req.body.arg10;
    const id = req.body.arg11;

   /*....some code.....*/
    res.sendFile(__dirname+"/desktop2-24.html");

    })

我在 vs 控制台中得到這樣的響應

{
  flg: 0,
  st: 200,
  data: {
    response: {
      RecID: '888',
      AssetNo: 1234,
      AssetSpace: 1000,
      AssetDate: '8/8',
      AssetDist: 'narjes',
      AssetState: 'altaeawun',
      AssetStatus: 'valid',
      F_owner: 'bushra',
      M_owner: 'mohammed',
      L_owner: 'almansour',
      OwnerID: '******1010'
    }
  }
}

我用這個 app.post 將數據發回給用戶

   app.post('/desktop2-24', (req, res1) =>{
 const obj = JSON.parse(JSON.stringify(res3));
                res1.render("readAsset",{assetno: obj.data.response.AssetNo })
                
    /*...some code...*/
                        })
              .catch (err =>{
                console.log(err)
              })
      
          })
          
    
        })
  app.listen(3000);

這是我發送數據的 ejs 文件

<!DOCTYPE html> 
<html lang="en" dir="ltr">
    <head>
        <meta charsey="utf-8">
        <title> Read deed info</title>
        </head>
        <body>
            <p>assetno: <%= assetno %></p>
        </body>
</html>

所以基本上我所要做的就是在第二個 app.post 中定義一個空值的變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM