Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get Cannot read property 'toString' of undefined

I use this package. I've added these console.log's at the beginning of the slug function.

function slug(string, opts) {
    console.log('log 1: -------');
    console.log('log 2: ' + string);
    console.log('log 3: ' + typeof string);
    string = string.toString();
    ....
 }

Here is the output:

log 1: -------
log 2: Yüzey Aktif Maddeler
log 3: string
/Users/------/seeder/node_modules/mongodb/lib/utils.js:99
    process.nextTick(function() { throw err; });
                                  ^

TypeError: Cannot read property 'toString' of undefined
    at slug (/Users/------/seeder/node_modules/slug/slug.js:16:20)
    at Object.createSlug (/Users/------/seeder/seeder/utils.js:4:9)
    at getContent (/Users/------/seeder/seeder/content-seeder.js:345:22)
    at topic.contents.forEach.content (/Users/------/seeder/seeder/content-seeder.js:425:31)
    at Array.forEach (native)
    at /Users/------/seeder/seeder/content-seeder.js:424:21
    at /Users/------/seeder/node_modules/mongodb/lib/collection.js:523:5
    at /Users/------/seeder/node_modules/mongodb/lib/collection.js:701:5
    at handleCallback (/Users/------/seeder/node_modules/mongodb/lib/utils.js:96:56)
    at executeCommands (/Users/------/seeder/node_modules/mongodb/lib/bulk/ordered.js:405:12)

Even if type of string parameter is a string, string.toString() throws a TypeError. It says string is undefined.

I'm not an experienced js developer. Why would this happen?

Edit: I call it from this function

function getContent(content, categoryId) {
    console.log(content.topicVideo);
    return {
        // _id: Random.id(),
        // other properties
        contentSlug: slug(content.topicVideo.contentName, {
            lower: true
        })
    };
}

New logs:

{ id: '197505065',
  categorySort: 18,
  categoryName: 'Hayatımızda Kimya',
  contentSort: 2,
  contentName: 'Yüzey Aktif Maddeler',
  fileName: 'KonuAnlatimi.mp4' }

Edit:

I've added a log at the end of the slug function.

    console.log(result);
    return result;
};

This prints the results and then throws error.

log 1: -------
log 2: Yüzey Aktif Maddeler
log 3: string
log 4: Yüzey Aktif Maddeler
yuzey-aktif-maddeler

Edit: I think this line gives a hint:

/Users/------/seeder/node_modules/mongodb/lib/utils.js:99
    process.nextTick(function() { throw err; });

I think it logs correct string, but fails on undefined one without logging the logs. Is this possible? I've found out that one of content.topicVideo.contentName is actually undefined. But error is thrown without logging it. Is this even possible?

like image 385
osrl Avatar asked Jan 03 '17 07:01

osrl


People also ask

How do you fix undefined properties Cannot be read?

To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.

What does it mean by Cannot read property of undefined?

What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value.

Can you read undefined property?

The "Cannot read properties of undefined" error occurs when trying to access a property on an undefined value. You often get undefined values when: accessing a property that does not exist on an object. accessing an index that is not present in an array.

How do I use toString in JavaScript?

The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.


1 Answers

To make sure that .toString() doesn't throw an error, you could:

string = "" + string; // amends the value to an string, even if its undefined or null etc

instead of:

string = string.toString();

However you could also check if the value of string is undefined or null. Also see here.

like image 138
fuma Avatar answered Oct 08 '22 19:10

fuma