question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

First uploaded file parses but next uploaded file does not parse

See original GitHub issue

Hello,

I’m trying to parse xlsx files into my database after uploading with multer. The first file uploaded has no issue being parsed and uploaded into SQL after initial server start but problem arises with the succeeding files coming in afterwards.

Main problem: new files after first not being parsed after uploading the first file.

My current code set up for the parsing looks like this:

export async function insertDbfHeader(headerPath: string, tableName: string) {
  const mySQLPool = getMySqlPool(databaseName);
  const connection = await mySQLPool.getConnection();
  const workbook = XLSX.readFile(path.resolve(__dirname, headerPath), { cellDates: true });
  const sheet_name_list = workbook.SheetNames;
  const xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);

  for (let i = 0; i < xlData.length; i++) {
    const rowObject: any = xlData[i];
    const values = Object.values(rowObject);
    let date = moment(new Date(values[0] as any)).format("YYYY/MM/DD");

    let savekey: any = values[20];
    savekey = parseInt(savekey);

    let ac_desc1 = values[5];
    let ac_desc2 = values[6];
    let cp_desc1 = values[10];
    let cp_desc2 = values[11];
    let cp_desc3 = values[12];
    let facility = values[22];

    const query = `INSERT INTO ${tableName} (AC_DESC1, AC_DESC2, CP_DESC1, CP_DESC2, CP_DESC3, SAVEKEY, FACILITY, DATE)  SELECT * FROM (SELECT  '${ac_desc1}' as v1, '${ac_desc2}' as v2, '${cp_desc1}' as v3, '${cp_desc2}' as v4, '${cp_desc3}' as v5, '${savekey}' as v6, '${facility}' as v7, '${date}' as v8 ) AS tmp WHERE NOT EXISTS (SELECT AC_DESC1, AC_DESC2, CP_DESC1, CP_DESC2, CP_DESC3, SAVEKEY, FACILITY, DATE FROM ${tableName} WHERE SAVEKEY = '${savekey}' AND AC_DESC1='${ac_desc1}' AND AC_DESC2='${ac_desc2}') LIMIT 1`;

    connection.query(query, (error, result) => {
      if (error) {
        console.error(error);
        return result;
      }
    });
  }

Any guidance would be greatly appreciated. I’ve been stuck on this for too long.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidKHahncommented, Apr 10, 2020

I figured it out it was definitely a SQL table structure set up and query method issue. Thanks for your help SheetJSDev!

0reactions
DavidKHahncommented, Apr 10, 2020

Yes my query sends in the parsed data into mySQL but with each new upload the old info gets added including the new data leaving me with duplicates.

I’m currently trying to avoid this and insert only the newest rows coming in from each uploaded file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null in Parse JSON when Upload in Forms is Optional
Solved: I have a flow that is triggered when a user submits a response to a Microsoft Form. A user has the option...
Read more >
Unable to upload file via File field · Issue #480 · parse ... - GitHub
I'm able to connect and modify any data in mongoDB, but File fiels does not work. It upon uploading it return {"error":"unauthorized"}.
Read more >
How to handle uploading and parsing files in your frontend ...
The solution to this problem is to test the mocked API and make sure it matches what we expect from the real API....
Read more >
Unable to upload files – Error parsing server response. - phpBB
My first guess would be that the phpbb/attachment/upload.php is corrupt/incomplete. Try reuploading that file. If that is the problem then ...
Read more >
Formidable doesnt form.parse() when uploading a file in Nodejs
This solution worked for me, it might be helpful for other dev. ... function(req, res, next){ // the uploaded file can be found...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found