MySQL X Dev API: session.sql 不是函数
MySQL X Dev API: session.sql is not a function
使用 @mysql/xdevapi 我正在尝试检索原始 SQL 查询的结果,但出现以下错误:
{"message":"Error validating selections: s.sql is not a function","level":"error"}
我正在关注 docs,但我不明白为什么会出现此错误。
失败代码如下...
const mysqlx = require('@mysql/xdevapi')
const logger = require('./logger')
const {
MYSQL_USER,
MYSQL_PASSWORD,
MYSQL_HOST,
MYSQL_PORT,
MYSQL_SCHEMA
} = process.env
const session = mysqlx.getSession({
user: MYSQL_USER,
password: MYSQL_PASSWORD,
host: MYSQL_HOST,
port: parseInt(MYSQL_PORT)
})
.catch(e => logger.error('Error connecting to database:', e))
exports.validateSelections = selections =>
session
.then(s => s
.getSchema(MYSQL_SCHEMA)
.sql('SELECT * FROM affiliate_links WHERE id = 1')
.execute()
)
调用代码...
const { body } = require('express-validator/check')
const { validateSelections } = require('../database')
const postcodeValidator = require('postcode-validator');
const logger = require('../logger')
const config = require('../../config/config')
module.exports = [
body('postcode')
.custom(postcode => {
const isValid = postcodeValidator.validate(postcode, 'UK')
logger.info(`Postcode value passed into postcode validator: ${postcode}`)
if(!isValid) {
return Promise.reject(config.errors.invalid_postcode)
}
return Promise.resolve()
}),
body('selections')
.custom(selections => {
logger.info(`Selections passed into validator: ${selections.toString()}`)
if (!Array.isArray(selections)) {
logger.debug('Selections is not an array')
Promise.reject(config.errors.invalid_survey_selection)
}
validateSelections(selections)
.then(result => {
// Check result and resolve
Promise.resolve()
})
.catch(e => {
logger.error(`Error validating selections: ${e.message}`)
Promise.reject(config.errors.invalid_survey_selection)
})
})
]
.sql()
方法在 Session
级别可用,在 Schema
级别不可用。此外,不确定您是否已经在这样做,但您希望在承诺链的末尾附加一个 catch()
处理程序,否则您将丢失 getSession()
调用后抛出的错误,这就是在这种情况下发生。
无论如何,您想执行以下操作之一(使用您自己的模式):
在查询本身中指定模式
exports.validateSelections = selections =>
session
.then(s => s
.sql(`SELECT * FROM ${MY_SCHEMA}.affiliate_links WHERE id = 1`)
.execute()
)
在 运行 查询之前切换到给定模式
exports.validateSelections = selections =>
session
.then(s => s
.sql(`USE ${MY_SCHEMA}`)
.execute()
.then(() => s
.sql('SELECT * FROM affiliate_links WHERE id = 1')
.execute()
)
)
使用 @mysql/xdevapi 我正在尝试检索原始 SQL 查询的结果,但出现以下错误:
{"message":"Error validating selections: s.sql is not a function","level":"error"}
我正在关注 docs,但我不明白为什么会出现此错误。
失败代码如下...
const mysqlx = require('@mysql/xdevapi')
const logger = require('./logger')
const {
MYSQL_USER,
MYSQL_PASSWORD,
MYSQL_HOST,
MYSQL_PORT,
MYSQL_SCHEMA
} = process.env
const session = mysqlx.getSession({
user: MYSQL_USER,
password: MYSQL_PASSWORD,
host: MYSQL_HOST,
port: parseInt(MYSQL_PORT)
})
.catch(e => logger.error('Error connecting to database:', e))
exports.validateSelections = selections =>
session
.then(s => s
.getSchema(MYSQL_SCHEMA)
.sql('SELECT * FROM affiliate_links WHERE id = 1')
.execute()
)
调用代码...
const { body } = require('express-validator/check')
const { validateSelections } = require('../database')
const postcodeValidator = require('postcode-validator');
const logger = require('../logger')
const config = require('../../config/config')
module.exports = [
body('postcode')
.custom(postcode => {
const isValid = postcodeValidator.validate(postcode, 'UK')
logger.info(`Postcode value passed into postcode validator: ${postcode}`)
if(!isValid) {
return Promise.reject(config.errors.invalid_postcode)
}
return Promise.resolve()
}),
body('selections')
.custom(selections => {
logger.info(`Selections passed into validator: ${selections.toString()}`)
if (!Array.isArray(selections)) {
logger.debug('Selections is not an array')
Promise.reject(config.errors.invalid_survey_selection)
}
validateSelections(selections)
.then(result => {
// Check result and resolve
Promise.resolve()
})
.catch(e => {
logger.error(`Error validating selections: ${e.message}`)
Promise.reject(config.errors.invalid_survey_selection)
})
})
]
.sql()
方法在 Session
级别可用,在 Schema
级别不可用。此外,不确定您是否已经在这样做,但您希望在承诺链的末尾附加一个 catch()
处理程序,否则您将丢失 getSession()
调用后抛出的错误,这就是在这种情况下发生。
无论如何,您想执行以下操作之一(使用您自己的模式):
在查询本身中指定模式
exports.validateSelections = selections =>
session
.then(s => s
.sql(`SELECT * FROM ${MY_SCHEMA}.affiliate_links WHERE id = 1`)
.execute()
)
在 运行 查询之前切换到给定模式
exports.validateSelections = selections =>
session
.then(s => s
.sql(`USE ${MY_SCHEMA}`)
.execute()
.then(() => s
.sql('SELECT * FROM affiliate_links WHERE id = 1')
.execute()
)
)