Untitled

                Never    
Text
       
// Assume arr contains the array of user objects
arr.forEach(user => {
    // Check if user exists in the database
    let sql = `SELECT * FROM users WHERE id = ${user.id}`; // Assuming 'id' is a unique identifier

    // Execute SQL query to check if the user exists
    // If the user exists, update their details
    // If not, insert the user into the database
    db.query(sql, (err, result) => {
        if (err) throw err;
        if (result.length > 0) {
            // User exists, update their details
            let updateSql = `UPDATE users SET name = '${user.name}', email = '${user.email}' WHERE id = ${user.id}`;
            db.query(updateSql, (err, result) => {
                if (err) throw err;
                console.log(`User ${user.id} updated successfully`);
            });
        } else {
            // User does not exist, insert them into the database
            let insertSql = `INSERT INTO users (id, name, email) VALUES (${user.id}, '${user.name}', '${user.email}')`;
            db.query(insertSql, (err, result) => {
                if (err) throw err;
                console.log(`User ${user.id} inserted successfully`);
            });
        }
    });
});

Raw Text