exports.index = function(req, res) {
moviedb.indexMovie()
.then(x => {
Movie.findAsync()
.then(responseWithResult(res))
.catch(handleError(res))
}
)
};
function responseWithResult(res, statusCode) {
statusCode = statusCode || 200;
console.log("Populating Response");
return function(entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}
The above code works perfectly fine, the returned function in the responsewithresult function, gets populated with the .then response. However, I was experimenting and tried this instead, but it did not work. Please explain why?
exports.index = function(req, res) {
moviedb.indexMovie()
.then(x => {
Movie.findAsync()
.then(x => {responseWithResult(res)}) // <-- this doesn't work
.catch(handleError(res))
})
};
It doesn't work since
.then(responseWithResult(res))
passes the result of responseWithResult
(which is a function that eventually returns a value) to the then
function, whereas
x => {responseWithResult(res)}
which is logically like
function(x) {
responseWithResult(res);
}
and when you put that inside then(...)
, nothing is returned.
You could fix that with
then(x => responseWithResult(res))
which is like
function(x) {
return responseWithResult(res);
}
but really you should refractor your entire function to make better use of promises, and have a cleaner code in the end:
exports.index = function(req, res) {
moviedb.indexMovie()
.then(() => Movie.findAsync())
.then(movie => responseWithResult(movie, res))
.catch(() => handleError(res))
};
function responseWithResult(entity, res, statusCode) {
statusCode = statusCode || 200;
console.log("Populating Response");
res.status(statusCode).json(entity);
}
according to the docs, I should be able to include the CSRF tokens in the header, grab them with jquery, and include them in the headers of my ajax calls. Unfortunately, including <html class='...
according to the docs, I should be able to include the CSRF tokens in the header, grab them with jquery, and include them in the headers of my ajax calls. Unfortunately, including <html class='...
Using GTM and Enhanced eCommerce: I need to track eCommerce on a third-party system and the required ID field is not called ID or transactionID ... it is called confirmID. I'm trying to retrieve a few ...
Using GTM and Enhanced eCommerce: I need to track eCommerce on a third-party system and the required ID field is not called ID or transactionID ... it is called confirmID. I'm trying to retrieve a few ...
How to extrude a quarter Circle Geometry (THREE.CircleGeometry) in Three.js? I create the quarter circle like this: var circle = new THREE.Mesh( new THREE.CircleGeometry( 25, 32, 0, Math.PI/2 ),...
How to extrude a quarter Circle Geometry (THREE.CircleGeometry) in Three.js? I create the quarter circle like this: var circle = new THREE.Mesh( new THREE.CircleGeometry( 25, 32, 0, Math.PI/2 ),...
I'm doing an exercise, but e.timestamp return initially return "-xxxx" value so the date.toDateString always return "Thu Jan 01 1970 click". I've looked for an answer also on w3schools but also its ...
I'm doing an exercise, but e.timestamp return initially return "-xxxx" value so the date.toDateString always return "Thu Jan 01 1970 click". I've looked for an answer also on w3schools but also its ...