2017-07-19 85 views
0

我有一個多重上傳表單,然後我使用Cloud Vision API處理圖像,對OCR結果做一些處理,並且我想重定向到另一個路由(/ next2)處理完所有文件後。Nodejs表示異步回調重定向到另一個路由

我編輯我的async.forEach代碼,但我得到了一個

TypeError: Cannot read property '0' of undefined 

我得到了什麼錯?

app.post('/vision/upload', upload.array("photos", 10), function(req, res) { 
    async.forEach(req.files, function (file, cb) { 
     var post = {url: file.location}; 
     connection.query('SET FOREIGN_KEY_CHECKS=0;', function (err) { 
      if (err) throw err; 
     }); 
     connection.query('SELECT * FROM documents WHERE documents.url = ?', file.location, function (err, res54) { 
      var o2 = isEmpty(res54); 
      var m9 = {}; 
      if (o2) { 
       connection.query('INSERT INTO documents SET ?', post, function (err, res5) { 
        if (err) throw err; 
        DocumentsNextPage.push(res5.insertId); 
       }); 
      } else { 
       connection.query('SELECT * FROM documents WHERE documents.url = ?', file.location, function (err, res9) { 
        m9 = res9; 
       }); 
       connection.query('UPDATE documents SET ? WHERE ?', [{url: file.location}, {url: file.location}], function (err) { 
        if (err) throw err; 
        DocumentsNextPage.push(m9[0].id); 
       }); 
      } 
      if (err) throw err; 
     }); 
     const req2 = new vision.Request({ 
      image: new vision.Image({ 
       url: file.location 
      }), 
      features: [ 
       new vision.Feature('DOCUMENT_TEXT_DETECTION', 10), 
      ] 
     }); 
     DocumentsNextPage.length = 0; 
     vision.annotate(req2).then((res2) => { 
      p1 = JSON.stringify(res2.responses); 
     p1up = p1.toUpperCase(); 
     x7 = res2.responses[0].textAnnotations; 
     console.log(x7); 
     }) 
     occurrencesText = new Occurrences(p1up, {ignored: arrayIgnoredWords}); 
     var tt1 = occurrencesText.getSorted(); 
     var oc1 = toArray(tt1); 
     var oc2 = unique(oc1); 
     for (var i = 0; i < 10; i++) { 
      occurencesResults.push(oc2[i][0]); 
      var postOccu = {name: oc2[i][0], active: 0, isOccurenceMeta: 1, url: file.location}; 
      connection.query('REPLACE INTO metas SET ?', postOccu, function (err) { 
       if (err) throw err; 
      }); 
     } 
     connection.query(queryString, function (err, rows, fields) { 
      if (err) throw err; 
      for (var i in rows) { 
       var fuse = new Fuse(x7, options); 
       var result = fuse.search(rows[i].name); 
       var t1 = isEmpty(result); 
       if (t1) { 
       } else { 
        arrayResults.push(rows[i].name); 
        var posTag0 = {name: [rows[i].name], active: 0, isOccurenceMeta: 0, url: file.location}; 
        connection.query('INSERT INTO metas SET ?', posTag0, function (err) { 
         if (err) throw err; 
        }); 
       } 
      } 
      connection.query('SELECT * FROM documents INNER JOIN metas ON documents.url = metas.url WHERE metas.url = ? GROUP BY metas.name ORDER BY documents.url DESC', file.location, function (err, res99) { 
       if (err) throw err; 
       for (var i in res99) { 
        if (res99[i].id != undefined) { 
         resultMetasDocs[i] = {'documents_id': res99[i].id, 'metas_id': res99[i].id_meta}; 
        } 
       } 
      }); 
      for (var i in resultMetasDocs) { 
       var documentHasMetas = resultMetasDocs[i]; 
       connection.query('REPLACE INTO documents_has_metas SET ?', documentHasMetas, function (err) { 
        if (err) throw err; 
       }); 
      } 
     }) 
    }) 
     cb(); 
    }, function() { 
     res.redirect('/next2'); 
    }); 

回答

0

幾個問題:

var m9 = {}; 您已經定義m9是一個對象,但您稍後再試,如果它是一個數組來訪問其成員。確保m9有一個你想要訪問的屬性,並且是正確的type

connection.query('SELECT * FROM documents WHERE documents.url = ?', file.location, function (err, res9) { 
       m9 = res9; 
      }); 
      connection.query('UPDATE documents SET ? WHERE ?', [{url: file.location}, {url: file.location}], function (err) { 
       if (err) throw err; 
       DocumentsNextPage.push(m9[0].id); 
      });` 

你大概從一個異步操作訪問具有概不就應該得到這一結果的異步操作的狀態知識另一個回調中的結果。

connection.query('SELECT * FROM documents WHERE documents.url = ?', 
file.location, function (err, res9) { 
    m9 = res9; 
    connection.query('UPDATE documents SET ? WHERE ?', [{url: 
    file.location}, {url: file.location}], function (err) { 
     if (err) throw err; 
     /* 
     because this callback is inside the callback of the former 
     operation, we can be sure that we will have access to the 
     results from that operation `m9` in this case 
     */ 
     DocumentsNextPage.push(m9[0].id); 
    }); 
}); 
+0

謝謝!我仍然得到了同樣的錯誤TypeError:'無法讀取/Users/florian/Downloads/multer-master/app.js:144:42 在/ Users/florian/Downloads/multer-master/node_modules/async/dist/async.js:3083:16' 144:42與此有關for for(var i = 0; i <10; i ++)occurencesResults.push(oc2 [i] [0] ); var postOccu = {name:oc2 [i] [0],active:0,isOccurenceMeta:1,url:file.location};如果(錯誤)拋出err; });如果(err)拋出錯誤,則返回0。 }' –

相關問題