@mysql/xdevapi 如何在 nodejs 中调试“插入的行中的字段数错误”?

问题描述

我使用新的 x-authentication 成功连接到本地 MysqL 服务器。我有一个具有以下架构 (DDL) 的产品表:

CREATE TABLE `products` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,`name` varchar(45) NOT NULL,`description_he` text,`description_en` text,`display_name_he` varchar(45) DEFAULT NULL,`display_name_en` varchar(45) DEFAULT NULL,`image_path` varchar(255) DEFAULT NULL,`price` smallint unsigned NOT NULL,`is_visible` tinyint NOT NULL DEFAULT '1',PRIMARY KEY (`id`),UNIQUE KEY `id_UNIQUE` (`id`),UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='contains information about products'

我尝试将以下 javascript 对象插入到此表中:

Product {
  productID: null,productName: 'product2',descriptionHE: 'תיאור המוצר',descriptionEN: 'product description',displayNameHE: 'מוצר',displayNameEN: 'Product',imagePath: 'assets/images/facebook-512.png',price: 400
}

我使用以下代码执行此操作:

let table = this.session.getSchema('...').getTable('products')
return table.insert(
  'id','name','description_he','description_en','display_name_he','display_name_en','image_path','price','is_visible')
    .values(
      5,product.name,product.descriptionHE,product.descriptionEN,product.displayNameHE,product.displayNameEN,product.imagePath,product.price,1)
    .execute()
    .catch((err) => {
      console.log(err);
    })

这会捕获以下错误

Error: Wrong number of fields in row being inserted
    at sqlResultHandler.BaseHandler.<computed> (C:\Users\...\node_modules\@MysqL\xdevapi\lib\Protocol\InboundHandlers\BaseHandler.js:119:17)
    at Array.entry (C:\Users\...\node_modules\@MysqL\xdevapi\lib\Protocol\InboundHandlers\BaseHandler.js:90:29)
    at WorkQueue.process (C:\Users\...\node_modules\@MysqL\xdevapi\lib\WorkQueue.js:75:19)
    at Client.handleServerMessage (C:\Users\...\node_modules\@MysqL\xdevapi\lib\Protocol\Client.js:208:21)
    at Client.handleNetworkFragment (C:\Users\...\node_modules\@MysqL\xdevapi\lib\Protocol\Client.js:252:14)
    at TLSSocket.<anonymous> (C:\Users\...\node_modules\@MysqL\xdevapi\lib\Protocol\Client.js:90:36)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
  info: {
    severity: 0,code: 5014,sqlState: 'HY000',msg: 'Wrong number of fields in row being inserted'
  }
}

我尝试使用参数并稍微简化表模式,但我还找不到错误。你知道我如何调试发送到 sql 服务器的查询以找出它失败的原因吗?

解决方法

问题是我使用了 product.name 而不是 product.productName。 结论,使用打字稿。 :)