Javascript required
Skip to content Skip to sidebar Skip to footer

Mongoose.connection.close Cannot Read Property Close of Udnefined

Author avatar

Resolving the JavaScript Promise Fault "TypeError: Cannot Read 'And then' of Undefined"

Deeksha Sharma

Front Cease Web Development

Introduction

Working with JavaScript Hope comes with its own array of errors, and a pop one is
TypeError: Cannot read belongings 'so' of undefined.

In this guide, nosotros will cover 2 code examples containing a bugs that crusade this TypeError and then refactor the code to resolve the consequence.

Example one

Say you have the function getTaxAmount(toll, taxRate) . Information technology takes two arguments, toll and taxRate , calculates the amount of tax using the inputs, and is expected to return a Promise that resolves to the calculated tax amount.

Next, call getTaxAmount() function with two arguments and log the returned value on the panel.

                                      1                    const                                                            getTaxAmount                                                            =                                                            (                    price                    ,                                          taxRate                    )                                                            =>                                                            {                                                            2                                                            Math                    .                    flooring                    (                    (                    cost                                        *                                          taxRate                    )                                                            /                                                            100                    )                    ;                                                            iii                                        }                    ;                                                            iv                    v                                        getTaxAmount                    (                    100                    ,                                                            12                    )                    .                    then                    (                    (                    taxAmount                    )                                                            =>                                                            console                    .                    log                    (                    taxAmount                    )                    )                    ;                                  

js

If yous run this buggy code on your browser console or using Node CLI, you will get this error:

                                      1                    TypeError: Cannot read property 'then' of undefined                                  

sh

Example 2

Here'due south another case. getGithubOrgs(url) is a function that takes a URL and uses Fetch API to go GitHub system data for a given user(deekshasharma). fetch() makes a network request to the destination URL and returns a Promise that resolves to a response object. The response is then parsed into a JSON. This function is expected to render a Promise that should resolve to JSON data.

The getGithubOrgs() function is then chosen with an argument containing a valid URL and logs the resulting JSON on the console.

                                      ane                    part                                                            getGithubOrgs                    (                    url                    )                                                            {                                                            two                                                            fetch                    (                    url                    )                    .                    then                    (                    (                    response                    )                                                            =>                                          response                    .                    json                    (                    )                    )                    ;                                                            iii                                        }                                                            iv                    5                                        getGithubOrgs                    (                                                            6                                                            "https://api.github.com/users/deekshasharma/orgs"                                                            7                                        )                    .                    then                    (                    (                    jsonData                    )                                                            =>                                                            console                    .                    log                    (                    jsonData                    )                    )                    ;                                  

js

When you run this code in the browser console, you lot will go an error:

                                      1                    TypeError: Cannot read property 'so' of undefined                                  

sh

What Causes This TypeError

TypeError - Cannot read property 'then' of undefined is thrown when the caller is expecting a Promise to be returned and instead receives undefined . Allow's consider the higher up examples.

In Example i, the getTaxAmount(price, taxRate) function, when chosen, should have returned a Promise that resolves to a value of 12 . Currently this part just calculates the tax amount using the two inputs and does non return a value. Hence, the undefined value is returned.

In Example two, the getGithubOrgs(url) part calls the Fetch API, which returns a Hope that resolves to a response object. This resulting Promise is received past the then() method, which parses the response to JSON using the json() method. Finally, so() returns a new Promise that resolves to JSON. Just yous may have noticed there is no return statement inside the getGithubOrgs(url) function, which means the resulting Promise from the then() method is never returned to the calling code.

How to Set up This Error

To resolve the result in both code examples, you'll need to refactor the functions. Let's await at them i past one.

Example one

The role getTaxAmount() should exist refactored to the code below.

Promise.resolve() returns a resolved Promise with the value of the revenue enhancement amount calculated by the office. So the calling code will ever receive a Promise every bit long as valid arguments were passed.

                                      1                    const                                                            getTaxAmount                                                            =                                                            (                    price                    ,                                          taxRate                    )                                                            =>                                                            {                                                            2                                                            return                                                            Promise                    .                    resolve                    (                    Math                    .                    floor                    (                    (                    price                                        *                                          taxRate                    )                                                            /                                                            100                    )                    )                    ;                                                            three                                        }                    ;                                                            4                    v                                        getTaxAmount                    (                    100                    ,                                                            12                    )                    .                    and so                    (                    (                    taxAmount                    )                                                            =>                                                            console                    .                    log                    (                    taxAmount                    )                    )                    ;                                  

js

Run this code on your browser console or Node CLI, and you should get an ouput of 12 .

Example 2

The function getGithubOrgs(url) should be refactored to include a return argument so that a Promise can be returned to the caller.

                                      i                    function                                                            getGithubOrgs                    (                    url                    )                                                            {                                                            ii                                                            render                                                            fetch                    (                    url                    )                    .                    then                    (                    (                    response                    )                                                            =>                                          response                    .                    json                    (                    )                    )                    ;                                                            3                                        }                                                            4                    5                                        getGithubOrgs                    (                    "https://api.github.com/users/deekshasharma/orgs"                    )                    .                    and so                    (                    (                    res                    )                                                            =>                                                            6                                                            panel                    .                    log                    (                    res                    )                                                            vii                                        )                    ;                                  

js

Conclusion

Whenever y'all see this TypeError while working with JavaScript Promise, the get-go step is to inspect the code that was expected to return a Promise . After all, you go this error when calling the then() method on a Promise . And the TypeError indicates you are calling then() on undefined , which is a hint that the Promise itself is undefined . The next step is to go and debug the code to figure out why a Hope is not returned. Nosotros looked at two different code examples to see what can potentially cause this mistake and how to resolve it.

You tin can read more than about Promise.then() on MDN.

fisonbobjespat.blogspot.com

Source: https://www.pluralsight.com/guides/javascript-promise-typeerror:-cannot-read-then-of-undefined