简体   繁体   English

App.post 返回空的花括号集

[英]App.post returning empty set of curly braces

I'm building an express.js application that just takes data from A mysql database and displays on the screen and I'm trying too implement an insert functionality so I can add too the database via browser, when I post the results too one of the route points I am returned empty braces and do not know why,any help at all would be appreciated.我正在构建一个 express.js 应用程序,它只从 A mysql 数据库中获取数据并显示在屏幕上,我也在尝试实现插入功能,因此当我发布结果时,我也可以通过浏览器添加数据库我返回空括号的路线点,不知道为什么,任何帮助都将不胜感激。

Index.js below this is addCountries.ejs下面的 Index.js 是 addCountries.ejs

 //brings you too add Country app.get("/addCountries", (req, res) => { res.render("addCountries") console.log("hello") }) //inserts data from add countries app.post("/addCountries", (req, res) => { sqlDAO.addCountry().then((data) => { res.render("addCountries", { addCountries: data }) console.log(req.body.co_name) console.log("hello") }).catch((error) => { res.send(error) console.log("hello") }) })
 <h1>Add Country</h1> <br> <br> <form action="/addCountries" method="POST"> <label for="cCode">Country Code:</label> <input type="text" id="cCode" name="co_code"><br><br> <label for="cName">Country Name:</label> <input type="text" id="cName" name="co_name"><br><br> <label for="CDetails">Country Details:</label> <textarea type="text" id="CDetails" name="co_details"></textarea> <input type="submit" value="Add"> </form>

SQLDAO.js SQLDAO.js

 var pool //creates pool based on database provided by project spec mysql.createPool({ connectionLimit: 3, host: 'localhost', user: 'root', password: 'password', database: 'geography' }).then((result) => { pool = result }).catch((error) => { console.log(error) }) var addCountry = function() { // returns new promise return new Promise((resolve, reject) => { // function that adds too database var myQuery = { sql: "INSERT INTO country VALUES (?, ?, ?)", values: [req.body.co_code, req.body.co_name, req.body.co_details] } pool.query(myQuery).then((data) => { resolve(data) console.log(data) }).catch(error => { reject(error) }) }) }

You will need to pass a reference to the request object to addCountry() .您需要将对请求 object 的引用传递给addCountry() This is because the addCountry() function inside the SQLDAO.js file does not have access to the request object.这是因为SQLDAO.js文件中的addCountry() function 无权访问请求 object。

Right now, in addCountry() the req variable is undefined, so when the SQL statement is compiled there is no data to insert.现在,在addCountry()req变量是未定义的,所以当 SQL 语句被编译时,没有数据可以插入。 If you look in the DB you'll likely see empty or no records being added.如果您查看数据库,您可能会看到空的或没有添加记录。

By passing in the request object, it has data to place into the db and that the db can return.通过传入请求 object,它有数据可以放入数据库并且数据库可以返回。

Edit both files like this:像这样编辑两个文件:

sqlDAO.addCountry(req)... then var addCountry = function(req) {... sqlDAO.addCountry(req)...然后var addCountry = function(req) {...

There are two reasons this is necessary:有两个原因是必要的:

  1. Inside your app.post() function both req and res are scoped locally to the function and are unavailable outside that function.在您的app.post() function 内部, reqres都在本地范围内限定为 function,并且在 function 之外不可用。

  2. Even if they were pseudo-global, variables are only available within the module they are created within.即使它们是伪全局的,变量也只能在创建它们的模块中使用。 So you must export that variable or, in this case, pass a reference to it to some other function within some other module.因此,您必须导出该变量,或者在这种情况下,将对它的引用传递给其他模块中的其他 function。

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

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