如何使用@ mysql / x devAPI连接Express中的路由参数?

这是我的代码:

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('/',){

        res.send('DB Is Connected');

    });

    module.exports = router;

}

通过邮递员,我运行以下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],toArray: [Function: toArray]

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

谢谢

zhang00shuai 回答:如何使用@ mysql / x devAPI连接Express中的路由参数?

看来您使用bind()的方式可能有问题。

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

因此,如果要过滤,您需要做的是提供实际的电子邮件地址作为bind()的第二个参数。我对Express感到有些生疏,但我认为以下方法可以解决问题:

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

请注意,bind()的第一个参数是不带:(冒号)前缀的占位符的名称。

免责声明:我是负责Node.js的MySQL X DevAPI Connector的主要开发者

,
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('/',){

        res.send('DB Is Connected');

    });

    module.exports = router;
本文链接:https://www.f2er.com/3041177.html

大家都在问