如何使用@mysql/x devAPI 连接我的快速路由参数?

How to connect my route parameter in express with @mysql/x devAPI?

这是我的代码:

var express = require('express');
var router = express.Router();
var mysqlx = require('@mysql/xdevapi');




router.use(`/:email`, function (req, res, next){
    mysqlx.getSession( {

        user: 'username', 
        password: 'password',
        host: 'localhost', 
        port: '33060' } )

        // .then(session => {
        //   console.log(session.inspect());
        // })

        .then(function (session) {
            var db = session.getSchema('nms2019local');
            var opsTable = db.getTable('operators');
            return opsTable
                .select (['email', 'admin'])
                .where('email like :email')
                .bind('email',':email')
                .execute (function (row) {
                    console.log(row);

                });    
        })
        .then(function (myResult){
            console.log(myResult);

        })
        .catch(function (err){
            console.log(err);
        })

        next()


    });


    router.use('/', function (req, res,){

        res.send('DB Is Connected');

    });

    module.exports = router;

}

通过 postman 我 运行 以下 GET 命令:

get /expressroutename/email/test@email.com 我在 nodemon 中得到以下内容:

GET /expressroutename/email/test@email.com 200 36.096 ms - 15
{
  getWarnings: [Function: getWarnings],
  getWarningsCount: [Function: getWarningsCount],
  fetchAll: [Function: fetchAll],
  fetchOne: [Function: fetchOne],
  getColumns: [Function: getColumns],
  getResults: [Function: getResults],
  nextResult: [Function: nextResult],
  toArray: [Function: toArray]
}

当我列出 .where 命令时

 //.where('email like :email')

并在 Postman 中输入以下内容,我在 nodemon 中获取:

GET /expressroutename/email 200 45.116 ms - 15
[ 'test@email.com', 1 ]
[ 'test1@email.com', 1 ]
{
  getWarnings: [Function: getWarnings],
  getWarningsCount: [Function: getWarningsCount],
  fetchAll: [Function: fetchAll],
  fetchOne: [Function: fetchOne],
  getColumns: [Function: getColumns],
  getResults: [Function: getResults],
  nextResult: [Function: nextResult],
  toArray: [Function: toArray]

我认为这是一个语法错误,但我已经通过文档输入了很多次,但它似乎不起作用。请帮忙。

谢谢

您的使用方式似乎有问题 bind()

基本上您提供了一个过滤条件 'email like :email',其中 :email 是一个占位符,应该使用 bind() 替换为特定值。但是,您正在将 ':email' (特定的 String 值)分配给占位符,我怀疑您在 operators table 中没有任何行 email = ':email'.这就是为什么当您删除过滤条件(由 where() API 指定)时您开始获得正确结果的原因,因为实际上不再应用过滤器。

所以,如果你想过滤,你需要做的是提供实际的电子邮件地址作为bind()的第二个参数。我对 Express 有点生疏,但我认为以下应该可以解决问题:

opsTable.select(['email', 'admin'])
  .where('email like :email')
  .bind('email', req.params.email)

注意 bind() 的第一个参数是没有 :(冒号)前缀的占位符的名称。

免责声明:我是 MySQL X DevAPI Connector for Node.js

的首席开发者
var express = require('express');
var router = express.Router();
var mysqlx = require('@mysql/xdevapi');




router.use(`email/:email`, function (req, res, next){
         const email = req.params.email        

     mysqlx.getSession( {

        user: 'username', 
        password: 'password',
        host: 'localhost', 
        port: '33060' } )

        // .then(session => {
        //   console.log(session.inspect());
        // })

        .then(function (session) {
            var db = session.getSchema('dbname');
            var TestTable = db.getTable('test');
            return TestTable
                .select (['email', 'admin'])
                .where('email like :email')
                .bind('email', email)
                .execute (function (row) {
                    console.log(row);

                });    
        })
        .then(function (myResult){
            console.log(myResult);

        })
        .catch(function (err){
            console.log(err);
        })

        next()


    });


    router.use('/', function (req, res,){

        res.send('DB Is Connected');

    });

    module.exports = router;