Mysql Knex.js在服务器端代码中验证查询结果
我有一个函数,用来检查我的MySQL数据库中是否已经存在牌照,但在返回时,结果如下: 结果:[对象]。您实际上如何获得这个knex查询的响应并对其进行解析以检查是否存在牌照Mysql Knex.js在服务器端代码中验证查询结果,mysql,node.js,knex.js,Mysql,Node.js,Knex.js,我有一个函数,用来检查我的MySQL数据库中是否已经存在牌照,但在返回时,结果如下: 结果:[对象]。您实际上如何获得这个knex查询的响应并对其进行解析以检查是否存在牌照 var licensePlateExists = function(licensePlate){ knex.select('plate').from('licensePlates').where({'plate': licensePlate}).limit(1).then(function(result){ co
var licensePlateExists = function(licensePlate){
knex.select('plate').from('licensePlates').where({'plate': licensePlate}).limit(1).then(function(result){
console.log("Result: " + result);
if(!result){
return true;
}
return false;
}).catch(function(error) {
console.error(error);
});
}
我想我可能有一个与查询本身相关的错误,但我在MySQL CLI中测试了原始查询字符串,它按预期输出了行。也许在knex查询生成器中可以进行排序
注意,这不会出错,它会一直执行。尝试使用
knex.select('plate').from('licensePlates').where('plate', licensePlate)
实际上使用计数查询会更好您是如何使用此函数的?另外,来自console.log的示例输出将帮助我们更好地理解正在发生的事情。尝试使用
console.dir(结果)记录此日志
或dovar stringResult=JSON.stringify(结果)代码>尝试这些console.dir(结果);[{plate:'VDS-1111-MA'}]和console.log(JSON.stringify(result));[{“plate”:“VDS-1111-MA”}]太棒了,这确实有效。我的意思是你可以随时优化你的查询<代码>从表中选择计数(1),其中键=值代码>结果应返回0或1。“0”表示“不存在”,而“1”表示“存在”。