0

How am I working with nodejs, mysql, forEach trying to save the email in my DB with a unique id, using the textarea tag, based on this question

Insert multiple email to mysql using single textarea

For-each over an array in JavaScript

javascript split on non-alphanumeric an keep delemiters at start

<form class="" action="/registration/instudent/{{id_school}}/{{mat}}/{{grade}}" method="post">

<textarea name="email" ></textarea>            
<button class="btn btn-lg">Save</button>

</form>

Using the example in PHP base, in my example.js file I have this code, try to save all three variables.

router.post('/instudent/:id_school/:mat/:grade', isLoggedIn, async (req,res) => {
  const { id_school, mat, grade } = req.params;
  const { email } = req.body;

  var uStudent = {
    id_school,
    mat,
    grade,
    email
}

Object.keys(uStudent).forEach(function (key){
    console.log(uStudent[key]);
    db.query('INSERT INTO date set ?', uStudent);
});
  //res.redirect('/');
});

When saving to my database, incorrectly insert the texts. In my database it inserts me in this way.

enter image description here

Any guide to see what I am doing wrong and that every email in the textarea tag gets a unique id?

correct form enter image description here

sheila.ars
  • 57
  • 1
  • 7
  • I'm presuming that data comes in like `"example1@gmail.com\nexample2.gmail.com\n..."` which means you need to split on `\n` and iterate over that array. – tadman Jan 27 '20 at 23:34
  • I am trying with the split `const x = uStudent.split(\n);` property but it tells me this: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of undefined – sheila.ars Jan 28 '20 at 00:54
  • You're going to have to give us more context about that. I'm supposing it should be `email.split(/\n/)` but that's just speculation without knowing the format you're receiving it in. – tadman Jan 28 '20 at 02:00
  • Currently as I show the code saves the data in this way. [https://drive.google.com/file/d/1ahGNCxDoB5KzxZ0XLq_PYFb8vXGHFkG9/view?usp=sharing](s1) what I want is to insert the data in this way [https://drive.google.com/file/d/1wl_8jc7PQ6zvKPhp0qkcVscrx_wY1Qxh/view?usp=sharing](s2) – sheila.ars Jan 28 '20 at 02:19
  • A Google Doc link doesn't help. It's always best to keep your question self-contained so people don't need to skip around to get all the information they need to answer it. – tadman Jan 28 '20 at 02:41
  • You need to split the incoming data and insert *N* times depending on what the data dictates. It looks like you're doing one insert. The way the code is structured now is quite confusing as you're doing what looks like multiple inserts per student for no reason. Like that loop over the keys doesn't even get used. – tadman Jan 28 '20 at 02:42

1 Answers1

1

I can't find where in the code you are setting the email value to be save.

there are some question here:

  1. You are looping base on your object keys(is this the correct behavior)?
  2. where did you update the uStudent to update its email?

I think you should loop based on splitted email:

router.post('/instudent/:id_school/:mat/:grade', isLoggedIn, async (req,res) => {
    const { id_school, mat, grade } = req.params;
    const { emails } = req.body;

    const uStudent = {
        id_school,
        mat,
        grade
    };

    // loop base on email
    let _emails = emails.split(/\r?\n/);
    _emails.forEach(email => {

        // update uStudent email field
        uStudent.email = email;

        // insert the uStudent
        db.query('INSERT INTO date set ?', uStudent);
    });
});

user2398347
  • 102
  • 3
  • I have corrected the code, an apology for some commented codes that I have not uploaded the correct one, but the email was passed along with the other variables. [https://drive.google.com/file/d/1_jvH2M0SgKZv1JHywgvc8De28__QQWuI/view?usp=sharing](Split) I get this error TypeError: uStudent.split is not a function – sheila.ars Jan 28 '20 at 01:56
  • @sheila.ars you should not split the uStudents, instead split the `emails`. then loop them to insert those emails in differenct row. kindly check my answer. – user2398347 Jan 28 '20 at 05:53
  • You were right, once you mentioned that you should not divide the `uStudents`, but divide them `emails`, I understood better, thank you very much for your help and patience. – sheila.ars Jan 28 '20 at 06:28