0

I have a multer multi-upload form, then i process the images with the Cloud Vision api, do some process on the OCR result and i want to redirect to another route (/next2) after ALL the files are processed.

I edited my code with async.forEach but i got a

TypeError: Cannot read property '0' of undefined

What i got wrong ?

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');
    });
Florian VIDAL
  • 1,601
  • 2
  • 9
  • 8

1 Answers1

0

Several Issues:

var m9 = {}; You have defined m9 to be an object but you later try to access its members as if it was an array. Ensure that m9 has a property you are trying to access and is the of correct 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);
            });`

You are probably trying to access results from one asynchronous operation inside another callback that has no knowledge regarding the state of the asynchronous operation that is supposed to get that result.

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);
  });
});
Nakilon
  • 34,866
  • 14
  • 107
  • 142
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57
  • Thanks ! I still got the same error TypeError: `Cannot read property '0' of undefined at /Users/florian/Downloads/multer-master/app.js:144:42 at /Users/florian/Downloads/multer-master/node_modules/async/dist/async.js:3083:16` The 144:42 is related to this 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}; connection.query('REPLACE INTO metas SET ?', postOccu, function (err) { if (err) throw err; }); }` – Florian VIDAL Jul 20 '17 at 13:38