I'm building a universal app using React, Redux, redux-saga, Express, Mongoose. I ran into a very strange issue: I have a simple API function that returns a Promise (the function is being used in a saga). Depending on whether it is the server or the client side it uses an axios lib to make a request or getting data from Mongoose model.
export function requestPosts() {
if (typeof require.ensure === 'undefined') {
const Post = require('../../../../../server/models/Post').default
return Post.find().lean().exec()
.then((result) => {
const posts = normalize(result, postListSchema)
return posts // data is correct
})
.catch((err) => { console.log(err) })
} else {
return axios.get('127.0.0.1/api/posts')
.then((result) => {
const posts = normalize(result.data, postListSchema)
return posts
})
.catch((err) => { throw err })
}
}
So the thing is when it is SSR and Mongoose is used, I'm getting an error:
(node:86450) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: e is not defined
(node:86450) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
All goes fine if I instead of Mongoose, return a simple promise with explicit data from json file or if I use axios to make an API request to the server itself. But if only I return a promise from Mongoose or MongoDB Native Driver (which I can obtain from the Mongoose model) I'm getting the error and request is hanging.
Maybe anyone have some thoughts?
Lars
German developer, who likes to play with everything that comes in his way
One or more of your promises return an exception. You have to catch it. I would use
.catch((error) => console.log(error))to check what the exception is.