Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: promise.then(...).then(...).then(...).then(...).catch is not a function in Node Js [duplicate]

Im getting this error and i dont know how to solve it. In a node js server, i'm using some .then() function on a promise and at the end i placed a .catch() function that for some reason is not recognized. I have seen in many places in tutorials that this is how an error is handled. I'm not using any external libraries.

The error :

 TypeError: promise.then(...).then(...).then(...).then(...).catch is not a function

This is my code:

exports.joinAlbum = function(req, res){


var promise = Album.findOne().where({'shortId': req.body.shortId}).exec(); // Returns a promise


promise.then(function(albumDoc){
    console.log('Then #1');

    .....

    }
    return albumDoc.save();  // Returns a promise
})



.then(function(albumDoc){
    console.log('Then #2');

    .....

    return User.findById(req.body.userId).exec(); // Returns a promise
})


.then(function(userDoc){
    console.log('Then #3');

     ........

    return userDoc.save();  // Returns a promise
})

//  Return a response
.then(function(savedUserDoc){
    console.log('Then #4');
    return res.status(200).json({success: true}); 
})

    //Error handler
.catch(function(err){
    console.log('Catch #1');
    console.log(err);
    res.status(200).json({success: false});
});
}

If .catch() is not the correct way to handle promise error, what do you suggest? Im trying to avoid using external libraries and prefer using native javascript

EDIT: solution

I added a npm module called blue-bird that helped me solve this.

like image 873
Vandervidi Avatar asked Nov 12 '15 17:11

Vandervidi


People also ask

Why is .then not a function?

The "then is not a function" error occurs when the then() method is called on a value that is not a promise. To solve the error, convert the value to a promise before calling the method or make sure to only call the then() method on valid promises. Here is an example of how the error occurs.

How do I use promises in node JS?

Consuming a promise For example, when we request data from a server via an API that returns a promise, we utilize the then() and catch() methods to consume whatever data is delivered. In the above code, the then() method is executed when the promise is fulfilled by the resolve() callback.

How do you handle Promise rejection?

If an error condition arises inside a promise, you “reject” the promise by calling the reject() function with an error. To handle a promise rejection, you pass a callback to the catch() function. This is a simple example, so catching the rejection is trivial.

How do I return a Promise from a function?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.


1 Answers

It looks like you're using Mongoose, which returns its own Promise, not the ES6 promise which includes a catch function. A mongoose Promise has no catch function. You can overwrite the default Promise that Mongoose uses, fortunately:

http://eddywashere.com/blog/switching-out-callbacks-with-promises-in-mongoose/

like image 107
dcochran Avatar answered Nov 10 '22 19:11

dcochran