Please help- JS novice- Chrome error

Hi,

I’m a complete novice to programming and am working my way through the book Learning to Program, by Steven Foote. Can anyone please help me with why Chrome is telling me I’ve got these two errors:

Uncaught TypeError: Cannot read property ‘currentDate’ of undefined
Uncaught TypeError: Cannot read property ‘projectName’ of undefined

This is what I’ve written:

values.js

var kbValues = {
 projectName: 'kittenbook',
 versionNumber: '0.0.1',
 currentDate: new Date(), 
 currentTime: [kbValues.currentDate.getFullYear() + '-' + 
(kbValues.currentDate.getMonth() + 1)+ '-' +
kbValues.currentDate.getDate() + ' at ' + 
kbValues.currentDate.getHours() + ':' + 
kbValues.currentDate.getMinutes() + ':' + 
kbValues.currentDate.getSeconds()]
};

kittenbook.js

var userName = prompt('Hello, what\'s your name?');
document.body.innerHTML = '<h1>Hello, ' + userName + '!</h1>' + 
'<p>' + kbValues.projectName + '' + kbValues.versionNumber +
' accessed on: ' + kbValues.currentTime + '</p>';

manifest.json

{
	"manifest_version": 2,
	"name": "kittenbook",
	"description": "Replace photos on Facebook with kittens",
	"version": "0.0.1",
	"content_scripts": [
		{
			"matches": ["*://www.facebook.com/*"],
			"js": ["js/values.js","js/kittenbook.js"]
		}
	]
}

This is my first post, and I really am very new to this, so your understanding is appreciated :slight_smile:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Thank you for that, I’ll bear in mind for future posts. :slightly_smiling_face:

If you read the error’s you can see they have to do with the next 2 lines":
currentDate: new Date(),
is it wise to have the , so close to the () ?
projectName: ‘kittenbook’,
something here is wrong as well.

Thanks for the reply. :slightly_smiling_face:Yes, I can see those two lines are the problem, but I can’t ascertain what the problem is. I’ll try to move the , and see if that helps for that line. I’m open to any other suggestions.

remember, what is to the right of the assignment operator is evaluated, and then assigned to what’s to the left of it

currentTime: [kbValues.currentDate.getFullYear()

here kbValues has not yet been defined, so you get error that you can’t use a property of an undefined value

1 Like

All good, thank you for the comments, it’s been resolved. I needed to assign the date first.

var currentDate = new Date();

var kbValues = {
 projectName: 'kittenbook',
 versionNumber: '0.0.1',
 currentDate, 
 currentTime: [currentDate.getFullYear() + '-' + 
  (currentDate.getMonth() + 1)+ '-' +
   currentDate.getDate() + ' at ' + 
   currentDate.getHours() + ':' + 
   currentDate.getMinutes() + ':' + 
   currentDate.getSeconds()]
};

at this point you don’t even need the currentDate property inside the object. It depends on what you need to do with this, but it seems repetitive and you already have the data

1 Like

Yes, very true. Thanks @ilenia. Gradually getting my head around it all!