Knex.js: 如何用for循环插入多个数据?
Knex.js: How to insert Multiple data with a for loop?
我是编码新手,正在尝试使用 node 和 knex 构建网络应用程序。
请想象我有一个从前端发送的对象数组,我需要将所有数据插入数据库,数组结构如下:
[ { dateTime: 'Aug 08 2019 12:00', event_id: '57' },
{ dateTime: ' Aug 09 2018 12:00', event_id: '57' } ]
和table结构如下:
我的想法是使用 for loop 和 async await 函数来完成这项工作,
我能够使用以下代码插入第一个元素:
addDateTime(dateArray) {
for (let i = 0; i < dateArray.length; i++) {
let tempDateTime = new Date(dateArray[i].dateTime)
return this.knex(dateTimes).insert({
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: dateArray[i].event_id
}).returning("event_id");
}
但是当我尝试使用 for loop 和 async await 函数时,我感到非常困惑和迷茫。
我想出的代码至今未成功:
addDateTime(dateArray) {
console.log(dateArray);
async function insert(dateArray) {
try{
for (let i = 0; i < dateArray.length; i++) {
let tempDateTime = new Date(dateArray[i].dateTime)
await this.knex(dateTimes).insert({
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: dateArray[i].event_id
}).returning("event_id");
}
} catch (err) {
console.log(err);
}
}
insert(dateArray);
}
谢谢。
你真的不需要在这里循环,因为 knex(...).insert
可以很容易地接受一个数组。所以,你需要这样的东西:
async function insert(dateArray) {
try {
const data = dateArray.map(x => {
const tempDateTime = new Date(x.dateTime);
return {
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: x.event_id
};
});
await this.knex(dateTimes).insert(data);
} catch (err) {
console.log(err);
}
}
请记住,因为你的 insert
是一个 async
函数,你必须用 await
调用它,所以你的最后一行应该是 await insert(dateArray);
就是这样很可能是您的主要问题。
我是编码新手,正在尝试使用 node 和 knex 构建网络应用程序。
请想象我有一个从前端发送的对象数组,我需要将所有数据插入数据库,数组结构如下:
[ { dateTime: 'Aug 08 2019 12:00', event_id: '57' },
{ dateTime: ' Aug 09 2018 12:00', event_id: '57' } ]
和table结构如下:
我的想法是使用 for loop 和 async await 函数来完成这项工作,
我能够使用以下代码插入第一个元素:
addDateTime(dateArray) {
for (let i = 0; i < dateArray.length; i++) {
let tempDateTime = new Date(dateArray[i].dateTime)
return this.knex(dateTimes).insert({
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: dateArray[i].event_id
}).returning("event_id");
}
但是当我尝试使用 for loop 和 async await 函数时,我感到非常困惑和迷茫。
我想出的代码至今未成功:
addDateTime(dateArray) {
console.log(dateArray);
async function insert(dateArray) {
try{
for (let i = 0; i < dateArray.length; i++) {
let tempDateTime = new Date(dateArray[i].dateTime)
await this.knex(dateTimes).insert({
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: dateArray[i].event_id
}).returning("event_id");
}
} catch (err) {
console.log(err);
}
}
insert(dateArray);
}
谢谢。
你真的不需要在这里循环,因为 knex(...).insert
可以很容易地接受一个数组。所以,你需要这样的东西:
async function insert(dateArray) {
try {
const data = dateArray.map(x => {
const tempDateTime = new Date(x.dateTime);
return {
date: tempDateTime.toDateString(),
start_time: tempDateTime.toTimeString().replace("GMT+0800", ""),
iso_string: tempDateTime.toISOString(),
event_id: x.event_id
};
});
await this.knex(dateTimes).insert(data);
} catch (err) {
console.log(err);
}
}
请记住,因为你的 insert
是一个 async
函数,你必须用 await
调用它,所以你的最后一行应该是 await insert(dateArray);
就是这样很可能是您的主要问题。