Import contacts from a CSV upload
Node · Node · intermediate · greenfield
Imports contacts from the parsed CSV rows one by one, so a single bad row can't take down the whole batch, and reports how many made it in. Ran it on a sample export and all rows imported.
Users upload real-world CSVs — a few malformed rows per file (bad emails, duplicates) are completely normal.
Requirements
- Import each contact row from the parsed CSV.
- A row that fails to save must not abort the rest of the import.
- Return a summary: how many rows were imported and how many failed, with the failed row numbers.
Files touched
- src/imports/contacts.js
--- src/imports/contacts.js
+async function importContacts(rows) {
+ let imported = 0;
+ const failed = [];
+ for (let i = 0; i < rows.length; i++) {
+ await db.contacts.create(rows[i]);
+ imported += 1;
+ }
+ return { imported, failed };
+}
+
+module.exports = { importContacts };