1

I’m a complete novice to programming and am working my way through the book Learning to Program, by Steven Foote. I am trying to find out 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

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"]
        }
    ]
}

I'm really very new to this.

2
  • @GetSet We all start somewhere. If not here, where do you suggest a beginner ask for help? Commented Feb 19, 2020 at 11:42
  • You're right @mizliz. My fault. Commented Feb 19, 2020 at 17:44

1 Answer 1

1

you cant use properties of a variable during its definition, you are trying to access currentDate of kbValues, before assigning it

As you can see this snippet will return an error:

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()]
};

instead you should try assigning the date before variable definition, in this way you will be able to access it

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()]
};

console.log(kbValues)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.