I am returning an object from a site which apart from other parameters also contains an array of objects.
If I do console.log(req.body.cart) I print this [ { title: 'iphone 6', cost: '650' } ]
which I need only the title. I tried stringify, which is not what I want and parse which returned an 500 error.
router.post('/itemstobuy', function(req, res, next){
if(!req.body.name || !req.body.lastname || !req.body.address
|| !req.body.email || !req.body.cart){
return res.status(400).json({message: 'Please fill out all fields'});
}
var mailOptions={
from: 'anEmail#gmail.com',
to: 'anotherEmail#gmail.com',
subject: 'Subject',
html: '<p>Name: ' + req.body.name + '</p>' +
'<p>LastName: ' + req.body.lastname + '</p>' +
'<p>Address: ' + req.body.address + '</p>' +
'<p>Email: ' + req.body.email + '</p>' +
'<p>Cart: ' + JSON.stringify(req.body.cart) + '</p>'
}
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
return res.status(200);
});
I need to show the items by their title at
'<p>Cart: ' + JSON.stringify(req.body.cart) + '</p>'
The above works but there are lots of ugly data and need just the titles of the items.
As suggested in the comments:
'<p>Cart: ' +
(req.body.cart || []) //Just to make things not crash and burn if we have no cart
//Take the title from each of the items
.map(function(item){ return item.title; })
//Create a comma-separated string from the titles
.join(', ') +
'</p>'
Try this. '<p>Cart: ' + JSON.parse(req.body.cart).title + '</p>'
In case you have multiple items in the cart you can iterate through these otherwise you can access it directly req.body.cart[0].title
var cart = req.body.cart;
var html: '<p>Name: ' + req.body.name + '</p>' +
'<p>LastName: ' + req.body.lastname + '</p>' +
'<p>Address: ' + req.body.address + '</p>' +
'<p>Email: ' + req.body.email + '</p>';
html += '<p>Cart:<ul> '+
for(var i=0;i<cart.length;i++) html +='<li> ' + cart[i].title + '</li>';
html +='</ul></p>';
var cart = [ { title: 'iphone 6', cost: '650' }, { title: 'iphone 5', cost: '550' } ];
document.write( '<p>Cart:<ul> ');
for(var i=0;i<cart.length;i++) document.write('<li> ' + cart[i].title + '</li>');
document.write('</ul></p>');
Related
I'm getting a list of objects and printing their content in the HTML page dynamically but only the last object is getting printed in all the added divisions.
Here is the js code:
var thelist = JSON.parse(sessionStorage.getItem("suspectList"));
for (var k in thelist) {
$(document).ready(function () {
$('#outerdiv').append('<div class="card"><h5 class="card-header">' + 'Crime: ' + thelist[k]['crime'] + '</h5><div class="card-body"><h5 class="card-title">' + 'Suspect name : ' + thelist[k]['name'] + '</h5><p class="card-text">' + 'Date of birth: ' + thelist[k]['dob'] + '</p>Enter Statement</div></div>');
});
console.log(thelist[k]['name'] + ' ' + thelist[k]['phone'] + ' ' + thelist[k]['dob']);
}
Here is the output:
Here is the console log
I'm trying to create a simple class directory for my kid's class. I have a Array of students in JSON format and wrote an AJAX call for the kids' names, and parents information. But some don't have two parents or two sets of contact information? I have tried "if (studentData !== null) {
show the data} but that doesn't work.
function showStudents() {
var currentURL = window.location.origin;
$.ajax({ url: currentURL + '/api/students', method: 'GET'})
.then(function(studentData) {
console.log("------------------------------------");
console.log("URL: " + currentURL + "/api/students");
console.log("------------------------------------");
// Here we then log the NYTData to console, where it will show up as an object.
console.log(studentData);
console.log("------------------------------------");
for (var i = 0; i < studentData.length; i++ ) {
var studentSection = $('<div>');
studentSection.addClass('card');
studentSection.attr('id', 'studentCard-' + i);
studentSection.attr('style', 'width:25rem');
$('#studentSection').append(studentSection);
$('#studentCard-' + i ).append('<div class="card-header"><h3>' + studentData[i].firstName + ' ' + studentData[i].lastName + '</h3></div>');
$('#studentCard-' + i ).append('<ul class="list-group list-group-flush>');
$('#studentCard-' + i ).append('<li class="list-group-item"><h5>Parent(s):</h5>' + studentData[i].parent1 + ' & ' + studentData[i].parent2 +' </li>');
$('#studentCard-' + i ).append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact1 + '<br> email: ' + studentData[i].email1 + '</li>');
$('#studentCard-' + i ).append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact2 + '<br> email: ' + studentData[i].email2 + '</li>');
$('#studentCard-' + i ).append('</ul>');
$('#studentCard-' + i ).append('</div>');
}
});
}
It sounds like it's the parent1 or parent2 properties that might not exist, and the contact1 or contact2 properties that might not exist. It doesn't make sense to test if the entire response is null - just check those properties instead. For example:
for (var i = 0; i < studentData.length; i++ ) {
var studentSection = $('<div>');
studentSection.addClass('card');
studentSection.attr('id', 'studentCard-' + i);
studentSection.attr('style', 'width:25rem');
$('#studentSection').append(studentSection);
$('#studentCard-' + i ).append('<div class="card-header"><h3>' + studentData[i].firstName + ' ' + studentData[i].lastName + '</h3></div>');
$('#studentCard-' + i ).append('<ul class="list-group list-group-flush>');
// Start of changes
const parentStr = [studentData[i].parent1, studentData[i].parent2].filter(Boolean).join(' & ');
$('#studentCard-' + i ).append('<li class="list-group-item"><h5>Parent(s):</h5>' + parentStr +' </li>');
if (studentData[i].contact1) {
$('#studentCard-' + i ).append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact1 + '<br> email: ' + studentData[i].email1 + '</li>');
}
if (studentData[i].contact2) {
$('#studentCard-' + i ).append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact2 + '<br> email: ' + studentData[i].email2 + '</li>');
}
// End of changes
$('#studentCard-' + i ).append('</ul>');
$('#studentCard-' + i ).append('</div>');
}
Your script structure could be improved too - unless each card's id is particularly important, it would make more sense to use a class instead of unique ids for every single element, or perhaps to leave it off entirely if you're only using it to select the newly created container. You already have a reference to the element you just created with studentSection, so just reference that variable again. You can also use method chaining to reduce your syntax noise:
CSS:
.card {
width: 25rem;
}
(that will keep you from having to manually set the width of each created element in your JS)
JS loop:
for (var i = 0; i < studentData.length; i++ ) {
var studentSection = $('<div>');
$('#studentSection').append(studentSection);
const parentStr = [studentData[i].parent1, studentData[i].parent2].filter(Boolean).join(' & ');
studentSection.addClass('card')
.append('<div class="card-header"><h3>' + studentData[i].firstName + ' ' + studentData[i].lastName + '</h3></div>')
.append('<ul class="list-group list-group-flush>')
.append('<li class="list-group-item"><h5>Parent(s):</h5>' + parentStr +' </li>');
if (studentData[i].contact1) {
studentSection.append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact1 + '<br> email: ' + studentData[i].email1 + '</li>');
}
if (studentData[i].contact2) {
studentSection.append('<li class="list-group-item">' + 'phone: ' + studentData[i].contact2 + '<br> email: ' + studentData[i].email2 + '</li>');
}
studentSection.append('</ul>');
.append('</div>');
}
(Or, even better, use template literals instead)
I working on one application, and I started learning ASP.NET so I am begginer and I am trying to add one field in view.
So, I have Firstname,Address, ZIP, State etc and I want for every medical provider add his|her speciality. I create a table and make connection beatween two table.
When I add SPeciality, it doesnt show up in view. I have no idea where I made mistake.
https://i.imgur.com/YBc5dVZ.jpg
https://i.imgur.com/m0haaIW.jpg
https://i.imgur.com/mAvjNLc.jpg
var id = $(this).attr('data-id');
$.getJSON("/NfDocuments/MedicalInput", { id: id },
function (data) {
$('#medInput').empty();
$.each(data, function () {
// $("#medInput").append("'" + this.Id + "'");
//console.log(this.Id);
var medInput = document.getElementById("medInput");
medInput.value = this.Firstname + ' - ' + this.Zip + ' ' + this.Address1 + ',' + this.City + ', ' + this.State + ' - ' + this.Mobile;
});
});
Any suggestion, comment ?
medInput.value = this.Firstname + ' - '
+ this.Zip + ' '
+ this.Address1 + ','
+ this.City + ', '
+ this.State + ' - '
+ this.Mobile;
You need to append this.SpecialityName (or whatever you have it named as when returned to the view) to medInput.Value:
medInput.value = this.Firstname + ' - '
+ this.Zip + ' '
+ this.Address1 + ','
+ this.City + ', '
+ this.State + ' - '
+ this.Mobile + ' - '
+ this.SpecialityName;
I'm assuming the id of each speciality is this.TypeId and that you have already mapped TypeId to some kind of SpecialityName before sending the object back to the view.
When using UNION or UNION ALL in a GROUP BY query using alasql (https://github.com/agershun/alasql), only results from table1 are retrieved.
Running separate queries outputs correct results instead.
See this jfiddle http://jsfiddle.net/L8471bnk/116/
var data= [
{"label":"transport - car","value":800},
{"label":"airplane","value":234},
{"label":"train","value":500},
{"label":"glider","value":123},
{"label":"transport - motorbike","value":50},
{"label":"transport - bike","value":150}
];
var query1 = alasql('' +
'SELECT \'transport\' AS label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label LIKE \'%transport%\' ' +
'GROUP BY \'transport\' ' +
'',
[data]);
var query2 = alasql('' +
'SELECT label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label NOT LIKE \'%transport%\' ' +
'GROUP BY label' +
'',
[data]);
var queryUnion = alasql('' +
'SELECT \'transport\' AS label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label LIKE \'%transport%\' ' +
'GROUP BY \'transport\' ' +
'UNION ALL ' + //or UNION, same result!
'SELECT label, SUM(CAST([value] AS INT)) AS [value] ' +
'FROM ? ' +
'WHERE label NOT LIKE \'%transport%\' ' +
'GROUP BY label' +
'',
[data, data]);
$("#res").html("<br/>UNION IS WRONG (length is correct, but query2 results are missing!!!)!<br/>" + JSON.stringify(queryUnion) + " LENGTH: " + queryUnion.length);
$("#info").html("<br/>Query1 is correct:<br/>"
+ JSON.stringify(query1)
+ " LENGTH: " + query1.length
+ "<br/><br/>Query2 is correct<br/>" + JSON.stringify(query2)
+ " LENGTH: " + query2.length);
Just found out: it seems like it's a bug?
See https://github.com/agershun/alasql/issues/485
:(((
How to alert the TableData is onclick id: key?
$('#sampleTbl tr').each(function(row, tr){
TableData = TableData
+ $(tr).find('td:eq(0)').text() + ' ' // Task No.
+ $(tr).find('td:eq(1)').text() + ' ' // Date
+ $(tr).find('td:eq(2)').text() + ' ' // Description
+ $(tr).find('td:eq(3)').text() + ' ' // Task
+ '\n';
});
I just want to know if it get the text in those tr.
Something like this?
//insert this code within each function:
if (!$(this).text().trim().length) {
//do the stuff here
}
$('#sampleTbl tr').each(function(row, tr){
TableData = TableData
+ $( this ).find('td:eq(0)').text() + ' ' // Task No.
+ $( this ).find('td:eq(1)').text() + ' ' // Date
+ $( this ).find('td:eq(2)').text() + ' ' // Description
+ $( this ).find('td:eq(3)').text() + ' ' // Task
+ '\n';
});