我遇到了node(目前是v6)和mysql的问题。
Node Code
正在服务器端运行,没有出现任何错误。代码第一次工作;但是,随后时间name
和description
值作为空字符串输入,quantity
列作为0
(默认值)输入。
created
,pid
和id
(自动递增)列的值。我不确定为什么这个第一次工作正常,之后没有工作。
编辑:当我从addMany
获取输出的查询并将其直接输入到mysql中时,它每次都有效。
我每次都能正确使用它吗?任何帮助都非常感谢!
表架构:
CREATE TABLE `some_table` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(11) unsigned NOT NULL,
`qty` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`created` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `k_pid` (`pid`),
CONSTRAINT `fk_some_table_p_pid` FOREIGN KEY (`pid`) REFERENCES `p` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1337 DEFAULT CHARSET=utf8;
节点代码:
function addMany(items, callback) {
const itemsArray = Array.isArray(items) ? items : [items];
const params = itemsArray.map((item) => {
return [
item.pid,
item.quantity,
item.name,
item.description,
Math.floor(new Date() / 1000),
];
});
const queryStr = 'INSERT INTO some_table (`pid`, `quantity`, `name`, `description`, `created`) VALUES ?;';
db.executeQuery(queryStr, [params], callback);
}
首次运行:
const items1 = [
{ name: 'name__1', description: 'some desc 1', quantity: 1, pid: '17771' },
{ name: 'name__2', description: 'some desc 2', quantity: 1, pid: '17771' }
];
addMany(items1, () => {});
addMany
创建并运行此查询:
INSERT INTO some_table
(`pid`, `quantity`, `name`, `description`, `created`)
VALUES
('17771', 1, 'name__1', 'some desc 1', 1502913412),
('17771', 1, 'name__2', 'some desc 2', 1502913412);
MYSQL显示此数据:
id pid quantity name description created
6423 17771 1 name__1 some desc 1 1502913412
6424 17771 1 name__2 some desc 2 1502913412
第二次运行:
const items2 = [
{ name: 'name__3', description: 'some desc 3', quantity: 1, pid: '17771' },
{ name: 'name__4', description: 'some desc 4', quantity: 1, pid: '17771' }
];
addMany(items2, () => {});
addMany
创建并运行此查询:
INSERT INTO some_table
(`pid`, `quantity`, `name`, `description`, `created`)
VALUES
('17771', 1, 'name__3', 'some desc 3', 1502913412),
('17771', 1, 'name__4', 'some desc 4', 1502913412);
MYSQL显示此数据:
id pid quantity name description created
6423 17771 1 name__1 some desc 1 1502913412
6424 17771 1 name__2 some desc 2 1502913412
6425 17771 0 1502913412
6426 17771 0 1502913412