Search code examples
node.jsmemgraphdb

Error when loading csv file using Node.js in Memgraph


I am using Memgraph on Ubuntu. I was trying to load the csv file in my Node.js script. I have installed the neo4j-driver. I can not fid what could be the error in my code.

When I run my script, I get the following error:

Neo4jError: CSV file not found: file:…///updated_news.csv
at captureStacktrace (/home/tbmu370/workspace/memgraph/sandbox/node_modules/neo4j-driver-core/lib/result.js:611:17)
at new Result (/home/tbmu370/workspace/memgraph/sandbox/node_modules/neo4j-driver-core/lib/result.js:105:23)
at Session._run (/home/tbmu370/workspace/memgraph/sandbox/node_modules/neo4j-driver-core/lib/session.js:229:16)
at Session.run (/home/tbmu370/workspace/memgraph/sandbox/node_modules/neo4j-driver-core/lib/session.js:179:27)
at insertNodes (/home/tbmu370/workspace/memgraph/sandbox/memcsv.js:50:21)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
code: ‘Memgraph.TransientError.MemgraphError.MemgraphError’,
retriable: true
}

This part is my code. As you can see I've defined all of the constants and connections strings. I've checked and Memgraph is running. I've also used a small code to test bolt connection and it works. Just the import is not working.

const fs = require(‘fs’);
const csv = require(‘csv-parser’);
const neo4j = require(‘neo4j-driver’);
const path = require(“path”);
const { execSync } = require(‘child_process’);

// Function to parse the CSV file and return a Promise
function parseCSVFile(filePath) {
return new Promise((resolve, reject) => {
const results = ;
fs.createReadStream(filePath)
.pipe(csv())
.on(‘data’, (data) => {
results.push(data);
})
.on(‘end’, () => {
resolve(results);
})
.on(‘error’, (error) => {
reject(error);
});
});
}

// Main function to connect to Memgraph and create nodes
async function createNodes() {
const driver = neo4j.driver(‘bolt://localhost:7687’);//, neo4j.auth.basic(‘your-username’, ‘your-password’));
const session = driver.session();

try {
const csvDir = path.join(__dirname, “…/news.csv”);
console.log(Importing data from ${csvDir});
const data = await parseCSVFile(csvDir);
// Copy the CSV file to the Memgraph Docker container
execSync(‘sudo docker cp /home/tbmu370workspace/memgraph/updated_news.csv ca5ef0b5c281:/updated_news.csv’);
// Process each row from the CSV data
for (const row of data) {
const genre = JSON.parse(row.genre);
const tags = JSON.parse(row.tags);
const name = row.name;

  const query = `
    LOAD CSV FROM "file:..///news.csv" WITH HEADER IGNORE BAD AS row
    CREATE (news:News)
    SET news.genre = $genre,
        news.tags = $tags,
        news.title = $name;
  `;

  await session.run(query, { genre, tags, name });
  console.log(`Created node for ${name}, ${tags}, ${genre}`);
}
} catch (error) {
console.error(‘Error:’, error);
} finally {
session.close();
driver.close();
}
}

// Call the main function
createNodes();

Solution

  • It seems to me that the file is not located at the given path. Try to copy it to /usr/lib/memgraph/updated_news.csv. Once you do that update the code lines

    syncExec(‘sudo docker cp /home/tbmu370/workspace/memgraph/updated_news.csv f60e60b1c260:/usr/lib/memgraph/updated_news.csv’);
    

    and

    LOAD CSV FROM "/usr/lib/memgraph/updated_news.csv" WITH HEADER IGNORE BAD AS row
    

    And try to run your script again.