0

I try to create a

html element for each element in an array but It doesn't work can you help me ?

my node js code :

con.connect(function(err) {
 // if (err) throw err;
 console.log("Connected!")
 con.query( 'SELECT * FROM `commentairesapplicationscommunautaire` ', function (error, results, fields) {
 for (var i = 0; i < results.length; i++){
 var zeuzryueartt = results[i].contenu
 console.log(zeuzryueartt)
 res.end('<p> '+ zeuzryueartt +' </p>')
 }
 });
});

this code looks like : <p> rrr </p>

But is wrong for my case

Thanks

Sterling Archer
22.5k19 gold badges86 silver badges121 bronze badges
asked Jun 13, 2018 at 16:09
4
  • Where is res coming from? Is con.connect inside of a node handler? Regardless, you can only use res.end/send/json once per callback. You'll have to concatenate these into a single variable and send that back Commented Jun 13, 2018 at 16:14
  • I want this : <p> rrr </p> <p> with a another </p> <p> <p> etc </p> Commented Jun 13, 2018 at 16:14
  • @SterlingArcher the code is not complete you know Commented Jun 13, 2018 at 16:15
  • I have try but it doesn't work : like var htmlcode = '<p> ' + zeuzryueartt + ' </p>' Commented Jun 13, 2018 at 16:17

1 Answer 1

1

You are ending the response stream on the very first step. You need to only call end once you built entire response html.

con.connect(function(err) {
 // if (err) throw err;
 console.log("Connected!")
 con.query( 'SELECT * FROM `commentairesapplicationscommunautaire` ', function (error, results, fields) {
 const entireHTML = results.map(result => `<p>${result.contenu}</p>`).join('')
 res.end(entireHTML)
 });
});
Sterling Archer
22.5k19 gold badges86 silver badges121 bronze badges
answered Jun 13, 2018 at 16:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.