使用节点JS Express JS获取用户的密码返回未定义

hbc1234

我正在尝试实现一个用例,其中用户可以通过提供用户名来请求检查其密码。问题:Node js后端应用程序在查询完成之前返回结果。我查看了解释javascript异步方面的文章。但是,我仍然不确定,如果在两者之间涉及休息电话,是否可以应用该解决方案。

高水平流量:

  1. 前端js使用用户名调用rest api(node.js应用程序)以获取密码
  2. 节点应用调用数据库以获取密码
  3. 返回的密码显示在前端

码:

前端:

function getPassword() {//Called by clicking button on ui
var username = 'testuser'; //hardcoding for simplicity
  $.ajax({
    url: urlWhereNodeJsAppIsHosted,
    error: function(password) {
      console.log('error' + JSON.stringify(password));
    }, success: function(data) {
      console.log('success' + JSON.stringify(password)); // Displaying on console for now
    }
  });
}

后端(Node js应用):

const express = require('express');
const request = require('request');
const mysql = require('mysql');
const app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    port: '3306',
    user: 'dbuser',
    password: 'dbuserpassword',
    database: 'dbname'
});

connection.connect(function(err) {
  if (err) {console.log(err);}
});

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  next();
});

app.get('/verify', (req, res) => {
  var username = req.query.username;
  var password = 'noresult';
  connection.query(
    'SELECT * FROM userpassword where user=?, ${[username]}', 
    function(err, rows, fields) {
      password = extractpasswordfromrows(rows);//iterate and get result
    }
  );
  console.log(connection.threadId); //Value is not undefined
  res.send(200, { success: password });
});

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => console.log(`listening on ${PORT}`));

数据库:

userpassword(user, password) table with existing data.

我是javascript新手,也是异步思维。请帮助您的输入。谢谢。

拖拉机

该代码将password成功回调发送到外部,而无需等待查询完成。尝试

app.get('/verify', (req, res) => {
var username = req.query.username;
var password = 'noresult';
connection.query(
 'SELECT * FROM userpassword where user=?, ${[username]}', 
 function(err, rows, fields) {
   password = extractpasswordfromrows(rows);//iterate and get result
   res.send(200, { success: password }); // Ok, have password here
   }
 );
 // Not Ok, don't have password here
 console.log(connection.threadId); //Value is not undefined
});

我没有尝试放入校验代码来验证err回调中的参数是否真实,即使从逻辑上看是不可能的。

有关针对此错误的编码的一般处理,请参见“如何从异步调用返回结果”

在回调之外处理结果

为了避免嵌套代码,回调代码可以根据处理结果来解析或拒绝新创建的promise,使用promise的then/catch方法将结果和错误处理功能附加到promise

另外,在Express中,您可以在需要时提供多个回调(中间件函数),app.get并使用next调用前进到下一个中​​间件函数。以帖子为例(已编译但未经测试):

app.get('/verify', 

  (req, res, next) => {
    var username = req.query.username;
    connection.query(
      'SELECT * FROM userpassword where user=?, ${[username]}', 
       function(err, rows, fields) {
         if( err) {
           res.status(500).send("no result");
         }
         else {
           res.locals.rows = rows;
           next();
         }
       }
     );
  },

  (req,res) => {
     const password = extractpasswordfromrows(res.locals.rows); //iterate and get result
     res.send(200, { success: password });
  }
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Express JS 中访问用户信息返回未定义

使用Express JS未定义用户名

在节点js / express(localhost)上未定义的Cookie

节点/ Express JS“无法读取未定义的属性'用户名'”

React + Express.js URL参数在获取请求时返回未定义

Express.js-Mongo对象返回未定义

Express.js模块bodyparser返回未定义

使用FCM Firebase节点JS Express发送批量推送通知

Passport.js 和 Express Session - req.user 未定义导致 .isAuthenticted() 返回 false

我不断从 node js express post 请求正文中获取未定义

Express JS图像参照来源未定义

Express.js req.body未定义

Express.js无法读取未定义的属性“ req”

在express.js路由器中未定义“ this”

Express.js响应未定义错误

无法读取未定义的属性“ destroy”-Express JS Session

Express JS:无法读取未定义的属性“ lazyrouter”

Express + Passport.js:req.user未定义

在Express.js中未定义req.session

Express.js:未定义的 request.body

循环遍历请求体节点 js + express

节点js + express + jade迭代数组

节点中的Express JS安装错误

节点JS Express包含/导入/导出

节点JS + Express-异步请求

节点js express中的冲突路由

如何通过REST API微服务(使用Express构建)在节点js中使用MongoDB更改流

使用 React 在 app.js 中获取错误“返回未定义”(“数据未定义”)

类型错误:无法读取 node.JS 中未定义的属性“用户名”/express 无法发布到路由