Express和Mongodb多次插入相同的数据

Express and Mongodb insert same data multiple times

本文关键字:数据 插入 Mongodb Express      更新时间:2023-09-26

我对Express和Mongodb很陌生。我正在做的项目要求我:

  1. 取一个包含多个url的对象
  2. 下载url的内容并保存到云存储
  3. 为每个保存的文件生成链接
  4. 将这些链接保存到Mongodb中作为单独的文档

传入的对象看起来像这样:

{
    "id" : 12345678,
    "attachments" : [ 
        {
            "original_url" : "https://example.com/1.png",
        },
        {
            "original_url" : "https://example.com/2.png",
        },
        {
            "original_url" : "https://example.com/3.png",
        }
    ]
}

最终目标是有3个独立的文档像这样保存在mongodb上:

{
    "id" : 87654321,
    "some_other_data": "etc",
    "new_url" : "https://mycloudstorage.com/name_1.png"
}

我有一个像这样的简单循环:

for(var i = 0; i < original_data.attachments.length; i++){
    var new_url = "https://example.com/" + i + ".png";
    download(original_url, new_url, function(new_url){
        console.log(new_url)
        new_data.new_url = new_url;
        save_new_url_to_mongodb(new_data);
    });
}

和保存函数看起来像这样:

function save_new_url_to_mongodb (data, cb) {
  getCollection(collection, function (err, collection) {
    if (err) {
      return cb(err);
    }
    collection.insert(data, {w: 1, ordered: false}, function (err, result) {
      if (err) {
        return cb(err);
      }
      var item = fromMongo(result.ops);
      cb(null, item);
    });
  });
}
var download = function(original_url, new_url, callback){
  request.head(original_url, function(err, res, body){
    if(res === undefined){
        console.log(err);
    } else {
        var localUrlStream = request(original_url);
        var file = bucket.file(new_url);
        var remoteWriteStream = file.createWriteStream();
        var stream = localUrlStream.pipe(remoteWriteStream);
        stream.on('error', function (err) {
            next(err);
        });
        stream.on('finish', function(){
            callback(new_url);
        });
    }
  });
};

下载部分很好,我在云存储中得到3个不同的图像文件。console.log也给了我3个不同的新url。

问题是新保存的mongodb文档都有相同的new_url。有时如果原始数据中有更多的original_url,则一些新文档将无法保存。

Thanks to lot

这是您在for循环中分配new_url的范围问题。JavaScript闭包内部循环-简单的实际例子

一个解决方案是使用Array.Prototype.forEach,它本质上解决了范围问题,因为每次迭代都会为回调创建一个闭包

original_data.attachments.forEach(function(i) {
  var new_url = "https://example.com/" + i + ".png";
  download(original_url, new_url, function(new_url){
    console.log(new_url)
    new_data.new_url = new_url;
    save_new_url_to_mongodb(new_data);
  });
})