Reputation: 29
When i call the function outputCustomerbyId(customer_id) , it outputs the value of the getAddressbyId(address_id) function in a very weird way : A row above the console.log statement and adds undefined in its place. I have been struggling with this problem in my other functions too but if I can figure this out its the same procedure for the other functions too . Can you guys help me ?
Output :
Customer 71: Martin Scott([email protected])
287 Brant St. Apt 4A Waterdown, ON. R93G3P
Home Address : undefined
Joined: Sat Feb 18 2017 19:11:42 GMT-0500 (Eastern Standard Time)
I have a data object :
var allData = [
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
{type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
{type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "[email protected]", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "[email protected]", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "[email protected]", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "[email protected]", address_id: 1836, add_date: null}},
{type:"customer", data:{customer_id: 71, store_id:614, first_name: "Martin", last_name: "Scott", email: "[email protected]", address_id: 2727, add_date: null}},
{type:"customer", data:{customer_id: 24, store_id:614, first_name: "Jonathan", last_name: "Pym", email: "[email protected]", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 36, store_id:193, first_name: "Kaitlyn", last_name: "Adams", email: "[email protected]", address_id: 5464, add_date: null}},
{type:"customer", data:{customer_id: 73, store_id:297, first_name: "Melissa", last_name: "Bennett", email: "[email protected]", address_id: 4536, add_date: null}},
{type:"address", data:{address_id: 1023, address: "2895 Yonge St.", city:"Toronto", province:"ON", postal_code:"L4C02G"}},
{type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}},
{type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}},
{type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}},
{type:"address", data:{address_id: 2473, address: "391 Baker St. Apt 231", city: "Mississauga", province: "ON", postal_code: "M4T8S3"}},
{type:"address", data:{address_id: 1611, address: "183 City Ct.", city: "Hamilton", province: "ON", postal_code: "J3T9V2"}},
{type:"address", data:{address_id: 1836, address: "67 Rhymer Ave.", city: "Stouffville", province: "ON", postal_code: "L3C8H4"}},
{type:"address", data:{address_id: 2727, address: "287 Brant St. Apt 4A", city: "Waterdown", province: "ON", postal_code: "R93G3P"}},
{type:"address", data:{address_id: 5464, address: "11 New St. Apt 2B", city: "Brampton", province: "ON", postal_code: "L694R7"}},
];
Here is my CustomerDb object:
var CustomerDB = {
customers: [],
addresses: [],
stores: [],
insertData: function(someData){
for(var i=0;i<someData.length;i++){
if(someData[i].type=="store"){
this.addStore(someData[i].data);
}else if(someData[i].type == "customer"){
this.addCustomer(someData[i].data);
}else if(someData[i].type == "address"){
this.addAddress(someData[i].data);
}
console.log(someData[i].type);
}
},
addStore: function(storeObj){
this.stores.push(storeObj);
},
addCustomer : function(customerObj){
customerObj.add_date= new Date() ;
this.customers.push(customerObj);
},
addAddress : function(addressObj){
this.addresses.push(addressObj);
},
outputCustomerById: function (customer_id){
for(var i=0;i<this.customers.length;i++){
if(this.customers[i].customer_id===customer_id ){
console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");
console.log("Joined: "+this.customers[i].add_date+"\n\n");
}
}
},
getAddressById: function(address_id){
for(var j=0;j<this.addresses.length;j++){
if(this.addresses[j].address_id===address_id){
console.log(this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code);
}
}
},
outputAllCustomers: function( ){
console.log("All Customers\n\n");
for(var i=0;i<this.customers.length;i++){
console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
console.log("Home Address: "+this.addresses[i].address+" "+this.addresses[i].city+", "+this.addresses[i].province+". "+this.addresses[i].postal_code+"\n");
console.log("Joined: "+this.customers[i].add_date+"\n\n");
}
},
};
Calling functions :
CustomerDB.insertData(allData);
CustomerDB.outputCustomerById(71);
Upvotes: 0
Views: 310
Reputation: 150070
Your getAddressById()
function has a console.log()
inside it that runs before the function returns, so that is why the address appears on its own line in the console.
And the function doesn't explicitly return a value, so it returns undefined
, which is why this:
console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");
...is like saying this:
console.log("Home Address : "+ undefined +"\n");
Change the function to return the value in question rather than logging it:
getAddressById: function(address_id){
for(var j=0;j<this.addresses.length;j++){
if(this.addresses[j].address_id===address_id){
return this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code;
}
}
return ""; // address not found, so return a default value
}
(Note: the return
statement that I added at the end will only be reached if the address isn't found. I don't know what you want to do in that case, so I've just returned an empty string. You might want to return null
or undefined
or throw an error.)
Expand and run the following snippet to see it working. The only thing I've changed in your code is what I covered above plus commenting out the console.log()
in the insert function.
var allData = [
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch", address_id: 1023}},
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}},
{type:"store", data:{store_id: 193, name: "Scotiabank - Mississauga", address_id: 1757}},
{type:"customer", data:{customer_id: 26, store_id:297, first_name: "Dave", last_name: "Bennett", email: "[email protected]", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "[email protected]", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "[email protected]", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 63, store_id:297, first_name: "Steven", last_name: "Edwards", email: "[email protected]", address_id: 1836, add_date: null}},
{type:"customer", data:{customer_id: 71, store_id:614, first_name: "Martin", last_name: "Scott", email: "[email protected]", address_id: 2727, add_date: null}},
{type:"customer", data:{customer_id: 24, store_id:614, first_name: "Jonathan", last_name: "Pym", email: "[email protected]", address_id: 1611, add_date: null}},
{type:"customer", data:{customer_id: 36, store_id:193, first_name: "Kaitlyn", last_name: "Adams", email: "[email protected]", address_id: 5464, add_date: null}},
{type:"customer", data:{customer_id: 73, store_id:297, first_name: "Melissa", last_name: "Bennett", email: "[email protected]", address_id: 4536, add_date: null}},
{type:"address", data:{address_id: 1023, address: "2895 Yonge St.", city:"Toronto", province:"ON", postal_code:"L4C02G"}},
{type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}},
{type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}},
{type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}},
{type:"address", data:{address_id: 2473, address: "391 Baker St. Apt 231", city: "Mississauga", province: "ON", postal_code: "M4T8S3"}},
{type:"address", data:{address_id: 1611, address: "183 City Ct.", city: "Hamilton", province: "ON", postal_code: "J3T9V2"}},
{type:"address", data:{address_id: 1836, address: "67 Rhymer Ave.", city: "Stouffville", province: "ON", postal_code: "L3C8H4"}},
{type:"address", data:{address_id: 2727, address: "287 Brant St. Apt 4A", city: "Waterdown", province: "ON", postal_code: "R93G3P"}},
{type:"address", data:{address_id: 5464, address: "11 New St. Apt 2B", city: "Brampton", province: "ON", postal_code: "L694R7"}},
];
var CustomerDB = {
customers: [],
addresses: [],
stores: [],
insertData: function(someData){
for(var i=0;i<someData.length;i++){
if(someData[i].type=="store"){
this.addStore(someData[i].data);
}else if(someData[i].type == "customer"){
this.addCustomer(someData[i].data);
}else if(someData[i].type == "address"){
this.addAddress(someData[i].data);
}
//console.log(someData[i].type);
}
},
addStore: function(storeObj){
this.stores.push(storeObj);
},
addCustomer : function(customerObj){
customerObj.add_date= new Date() ;
this.customers.push(customerObj);
},
addAddress : function(addressObj){
this.addresses.push(addressObj);
},
outputCustomerById: function (customer_id){
for(var i=0;i<this.customers.length;i++){
if(this.customers[i].customer_id===customer_id ){
console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
console.log("Home Address : "+this.getAddressById(this.customers[i].address_id)+"\n");
console.log("Joined: "+this.customers[i].add_date+"\n\n");
}
}
},
getAddressById: function(address_id){
for(var j=0;j<this.addresses.length;j++){
if(this.addresses[j].address_id===address_id){
return this.addresses[j].address+" "+this.addresses[j].city+", "+this.addresses[j].province+". "+this.addresses[j].postal_code;
}
}
return "";
},
outputAllCustomers: function( ){
console.log("All Customers\n\n");
for(var i=0;i<this.customers.length;i++){
console.log("Customer "+this.customers[i].customer_id+": "+this.customers[i].first_name+" "+this.customers[i].last_name+"("+this.customers[i].email+")\n");
console.log("Home Address: "+this.addresses[i].address+" "+this.addresses[i].city+", "+this.addresses[i].province+". "+this.addresses[i].postal_code+"\n");
console.log("Joined: "+this.customers[i].add_date+"\n\n");
}
},
};
CustomerDB.insertData(allData);
CustomerDB.outputCustomerById(71);
Upvotes: 1