我正在使用mysql
将图像插入数据库。
将这些图像放入数据库的顺序很重要。我知道可以有一个order
列,但是由于项目的后勤原因,我无法做到这一点。
因此,使用Map,我保持了对象与结果的顺序:
Map {
0 => 'https://i.ebayimg.com/00/s/NTc5WDg0Ng==/z/dMkAAOSwHcpaoorf/$_03.JPG',
1 => 'https://i.ebayimg.com/00/s/NTc5WDc4NA==/z/hVkAAOSwQ19aoorl/$_03.JPG',
2 => 'https://i.ebayimg.com/00/s/NTc5WDgxMQ==/z/-kYAAOSwYBxaoorO/$_03.JPG',
3 => 'https://i.ebayimg.com/00/s/NTgwWDgxMQ==/z/seAAAOSwxo9aoorR/$_03.JPG',
4 => 'https://i.ebayimg.com/00/s/NTgwWDEwMzA=/z/p1AAAOSwW5laoorU/$_03.JPG',
5 => 'https://i.ebayimg.com/00/s/NzY0WDExMTQ=/z/p10AAOSwW5laoorY/$_03.JPG',
6 => 'https://i.ebayimg.com/00/s/NTE4WDc3Nw==/z/Yq0AAOSwVExaoorb/$_03.JPG',
7 => 'https://i.ebayimg.com/00/s/NTgwWDEwMzA=/z/mNMAAOSwfTVaoori/$_03.JPG' }
太好了。因此,现在将它们放入数据库中:
data.images.forEach(function(image, j) {
let insert_image_query = "INSERT INTO images (file, is_external) VALUES ('" + image + "', 1)";
mysqlQuery.executeQuery(insert_image_query).then(function (insert_image_response) {
});
});
但是它们并没有按顺序排列,有时是非常随机的。有什么我想念的吗?
答案 0 :(得分:0)
ECMA-262, 5th edition规范和MDN's Array.forEach()
page都显示了.forEach()
的算法,并且肯定会以升序索引顺序遍历数组元素(跳过从未分配值的索引),但是特定请求的执行时间可能会有所不同。您传递给.foreach()
的回调函数的每次调用不会持续相同的时间。它们都是按照您希望的顺序调用的,但是由于它们不是同步的,因此您的代码将不会等待第一个完成之后再执行下一个。
当然,某些浏览器可能无法正确实现该算法,但我不知道有哪些算法没有实现。
要修复这一问题,您可以将整个内容包装到异步函数中,并分别等待每个函数的执行
(async ()=>{
for (let image of data.images){
let insert_image_query = "INSERT INTO images (file, is_external) VALUES ('" + image + "', 1)";
await mysqlQuery.executeQuery(insert_image_query).then(function (insert_image_response) {
});
}
})();
或使用.reduce()
语法
data.images.reduce(async (lastItemsPromise, image) => {
await lastItemsPromise;
let insert_image_query = "INSERT INTO images (file, is_external) VALUES ('" + image + "', 1)";
return mysqlQuery.executeQuery(insert_image_query).then(function (insert_image_response) {
});
}, Promise.resolve());
答案 1 :(得分:0)
mysqlQuery.executeQuery()
是异步的,我将递归调用.then()方法中的另一个insert,以确保以正确的顺序插入数据,例如:
function insert(images, index = 0)
{
let insert_image_query = "INSERT INTO images (file, is_external) VALUES ('" + images[index] + "', 1)";
mysqlQuery.executeQuery(insert_image_query).then(function (insert_image_response) {
if (index + 1 < images.length)
insert(images, index + 1);
});
}
// first insert
insert(images);
或者,在循环中构建一个查询,然后执行它,这将提高性能,避免对数据库的许多请求:
const images = ['https://i.ebayimg.com/00/s/NTc5WDg0Ng==/z/dMkAAOSwHcpaoorf/$_03.JPG',
'https://i.ebayimg.com/00/s/NTc5WDc4NA==/z/hVkAAOSwQ19aoorl/$_03.JPG',
'https://i.ebayimg.com/00/s/NTc5WDgxMQ==/z/-kYAAOSwYBxaoorO/$_03.JPG',
'https://i.ebayimg.com/00/s/NTgwWDgxMQ==/z/seAAAOSwxo9aoorR/$_03.JPG',
'https://i.ebayimg.com/00/s/NTgwWDEwMzA=/z/p1AAAOSwW5laoorU/$_03.JPG',
'https://i.ebayimg.com/00/s/NzY0WDExMTQ=/z/p10AAOSwW5laoorY/$_03.JPG',
'https://i.ebayimg.com/00/s/NTE4WDc3Nw==/z/Yq0AAOSwVExaoorb/$_03.JPG',
'https://i.ebayimg.com/00/s/NTgwWDEwMzA=/z/mNMAAOSwfTVaoori/$_03.JPG'];
let image_array = [];
images.forEach(function(image, j) {
image_array.push("('" + image + "', 1)");
});
let insert_image_query = "INSERT INTO images (file, is_external) VALUES " + image_array.join(', ');
console.log(insert_image_query);
请注意,此查询容易受到SQL注入的攻击,但我不知道该库,也无法告知如何对查询进行参数设置