I'm getting an empty response ({}), whereas my expected response is of format:
{
locationResponse: "location foo",
forecastResponse: "forecast bar"
}
In my index file I have:
const {getCity} = require('./routes/city');
const {getForecasts} = require('./routes/forecast');
app.get('/forecasts', function (req, res) {
var location = getCity(req, res);
var forecast = getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
Inside forecast file I have the following, and a similar one is in city file:
module.exports = {
getForecasts: (req, res) => {
var result = //mySQL DB calls and processing
console.log("Inside getForecasts");
console.log(result); //actual result printed
return "Forecast";
}
UPDATE: So I added some logs right before each call's return statements and figured out that the logs are printed in the following order, which means, it is not working as expected because I have not considered the fact that they are asynchronous calls.
Inside index.js
undefined
undefined
Inside getForecasts
{result}
The problem here is that in your ./routes/forecast/ getForecasts method, you're telling the response to send, with the data "Forecast". You should only ever use res.send once per request, as this will resolve the response and return to the client.
Instead, your getForecasts method should just return whatever data you need, and your index file should handle the response. If you need getForecasts to handle a response too, perhaps because you're sending requests directly to a forecasts endpoint that doesn't require location data, then you can refactor your code so that both index and forecasts make a call to get the data you need. For example:
/* index.js */
const {getCity} = require('./data/city');
const {getForecasts} = require('./data/forecast');
app.get('/forecasts', function (req, res) {
var location = getCity();
var forecast = getForecasts();
res.send({locationResponse: location, forecastResponse: forecast});
});
/* data/forecast.js */
module.exports = {
getForecasts: () => {
return "Forecast";
}
};
/* data/city.js */
module.exports = {
getCity: () => {
return "City";
}
};
Then you can also have:
/* routes/forecast.js */
const {getForecasts} = require('../data/forecast');
module.exports = {
getForecasts: (req, res) => {
res.send(getForecasts());
}
};
The above may be overcomplicating things, but I made the assumption that if you're using a routes directory, you probably want route handlers to be stored there. Hope this helps.
Seems both of your getCity() and getForecasts() functions are async. These asynchronous functions return a promise rather actual response.
So you can use simple asysn/await or Promise.all in JS to solve the issue.
Option 1: Use await for the promise to resolve before logging the message to the console:
app.get('/forecasts', async function (req, res) {
var location = await getCity(req, res);
var forecast = await getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
Option 2: Use Promise.all() to wait for all the promises to have fulfilled.
app.get('/forecasts', function (req, res) {
var list = await Promise.all([getCity(req, res), getForecasts(req, res)]);
//these are logged as undefined
console.log("Inside index.js");
console.log(list[0]);
console.log(list[1]);
res.send({locationResponse: list[0], forecastResponse: list[1]});
});
You can make use of async/await syntax.
app.get('/forecasts', async function (req, res) {
var location = await getCity(req, res);
var forecast = await getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
Related
My self and another developer are working on an API using node.js and we are not advanced coders yet. We ran into problem. I will try and explain the problem here with sample references.
We have an API route that would be called on the frontend, please this is just a sample and not the real code. client said the codes should be private. We want to stop the function when an error is detected in another function we called. I am sure that we are not doing it rightly. Here is the API route for this post request and we called another function that we exported and imported here.
We simply want the httpCreateHarsh function to end if there is an error in the anotherFunction. With what we have, the error is seen in console.log when the user is not an admin for example but the httpCreateHarshfunction keeps running until it reaches the last line. Is this possible? Or is there another way we can structure the code to achieve this?
Shared sample of my code
const callAnotherFunction = require("../anotherfunction")
const httpCreateHarsh = async(req, res) => {
await callAnotherFunction(req, res);
return res.status(200).json('created')
}
//This is the second function we called:
const obj = {
status: 'success',
code: '244'
}
const anotherFunction = async(req, res) => {
if (req.body.user !== 'admin') {
return res.status(401).json('Unauthorized')
}
return obj
}
export default = anotherFunction
//The route here:
const express = require('express');
const router = express.Router();
const httpCreateHarsh = require('../httpCreateHarsh');
router.post("/harsh", httpCreateHarsh)
You couldn't return the res object in your second function.
To solve this problem you could throw exceptions and catch them in your handler function.
// request handler function
const httpCreateHarsh = async (req, res) => {
try {
await callAnotherFunction(req, res);
} catch (e) {
return res.status(401).json('Unauthorized')
}
return res.status(200).json('created')
}
const anotherFunction = asyn(req, res) => {
if (req.body.user !== 'admin') {
throw new Error('Unauthorized')
}
return obj
}
What you can do is you can wrap the code in httpCreateHarsh in an try...catch so whenever there is error inside it it will trigger the catch block and u exit the api.
const httpCreateHarsh = async(req, res)=>{
try{
await callAnotherFunction(req, res);
return res.status(200).json('created')
} catch(err){
return res.status(401).json('Unauthorized')
}
}
As an addition to this code you can return a promise from anotherFunction so that the catch block will be triggered once the promise is rejected.
For Exmaple:
const anotherFunction = async(req, res) => {
return new Promise(function(myResolve, myReject) {
if (req.body.user !== 'admin') {
myReject();
}
myResolve(obj);
});
}
If the code runs as you want it, it will generate the "cannot set headers after they are sent to the client" error, because you will be returning 2 responses
the first will be "unauthorized" by "anotherFunction" function and then the other response which is "created" of the current function "httpCreateHarsh".
what you should do instead is to call the "anotherFunction" as a middleware before moving to the "httpCreateHarsh" function.
it can be done this way:
// anotherfunction.js file containing the function you want to import
module.exports = {
async anotherFunction(req, res) {
if (req.body.user !== 'admin') {
return res.status(401).json('Unauthorized')
}
// this way, you can access this object from the "httpCreateHarsh" function by using req.body.obj
req.body.obj = {
status: 'success',
code: '244'
}
// this next indicates that there were no errors, and the next function will be called
next();
}
}
const httpCreateHarsh = async(req, res) => {
// do wathever you want here
return res.status(200).json('created')
}
//The route here:
const express = require('express');
const router = express.Router();
const httpCreateHarsh = require('../httpCreateHarsh');
const callAnotherFunction = require("../anotherfunction")
router.post("/harsh", (req, res, next) => callAnotherFunction(req, res, next), httpCreateHarsh)
I have the following files:
My routes - where the orders_count route lives:
routes/index.js
const express = require('express');
const router = express.Router();
const transactionsController = require('../controllers/transactionsController');
const ordersController = require('../controllers/ordersController');
const ordersCountController = require('../controllers/ordersCountController');
router.get('/transactions', transactionsController);
router.get('/orders', ordersController);
router.get('/orders_count', ordersCountController);
module.exports = router;
I then have my orders count controller living in the controllers directory:
controllers/ordersCountController.js
const ordersCountService = require('../services/ordersCountService');
const ordersCountController = (req, res) => {
ordersCountService((error, data) => {
if (error) {
return res.send({ error });
}
res.send({ data })
});
};
module.exports = ordersCountController;
My controller then calls my order count service which fetches data from another API.
services/ordersService.js
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = (req, res) => {
const url = ...;
const settings = { method: 'Get'};
fetch(url, settings)
.then(res => {
if (res.ok) {
res.json().then((data) => {
return data;
});
} else {
throw 'Unable to retrieve data';
}
}).catch(error => {
console.log(error);
});
}
module.exports = ordersCountService;
I'm trying to return the JSON response. I initially had it setup with requests but looking at the NPM site, it appears that it's depreciated so have been digging through how to use node-fetch.
I have tried both 'return data' and res.send({data}), but neither are solving the problem.
I am still new to this so I am likely missing something very obvious, but how come I am not sending the JSON back through so that it displays at the /api/orders_count endpoint?
I keep thinking I messed something up in my controller but have been looking at it for so long and can't seem to figure it out.
Any help would be greatly appreciated and if there is anything I can add for clarity, please don't hesitate to ask.
Best.
please learn promises and await syntax. life will be easier.
never throw a string. always prefer a real error object, like that : throw new Error('xxx'); that way you will always get a stack. its way easier to debug.
avoid the callback hell : http://callbackhell.com/
you need to decide if you want to catch the error in the controller or in the service. no need to do in both.
in the controller you call the service that way :
ordersCountService((error, data) => {
but you declare it like that :
const ordersCountService = (req, res) => {
which is not compatible. it should look like this if you work with callback style :
const ordersCountService = (callback) => {
...
if (error) return callback(error)
...
callback(null, gooddata);
here is an example to flatten your ordersCountService function to await syntax, which allows the "return data" you were trying to do :
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = async (req, res) => {
const url = ...;
const settings = { method: 'Get'};
try {
const res = await fetch(url, settings);
if (!res.ok) throw new Error('Unable to retrieve data');
return await res.json();
} catch(error) {
console.log(error);
}
}
module.exports = ordersCountService;
in fact i would prefer to error handle in the controller. then this woud be sufficient as a service
const fetch = require('node-fetch');
// connect to api and make initial call
const ordersCountService = async () => {
const url = ...;
const settings = { method: 'Get'};
const res = await fetch(url, settings);
if (!res.ok) throw new Error('Unable to retrieve data');
return await res.json();
}
module.exports = ordersCountService;
then you can call this funtion like this :
try {
const data = await ordersCountService(req, res);
} catch(err) {
console.log(err);
}
//or
ordersCountService(req, res).then((data) => console.log(data)).catch((err) => console.error(err));
so I am building a website and using nodejs to do a bunch of api calls and populate all the info. I have a homepage which has a sidebar where people can sort by 'categories'. but when I run my code instead of the categories being displayed in the html I get [object Object]. I have tried a whole bunch of things but still it only returns [object Object]. here's the code:
index.js
const express = require('express')
const router = new express.Router();
const categoriesFunction = require('../controllers/categories')
// get index page
router.get('', async (req, res) => {
let requestString = '/apps'
allApps = await openChannelRequest(requestString, req, res)
await res.render('index', {
categories: categoriesFunction,
// this is where I try to add the categories to the homepage
})
})
here's the controller where I'm grabbing all categories data and storing it. I'm pretty sure I can do this in the index.js page but a few weeks ago when I started this I made controllers for some reason. if that is not the best way to do this please let me know.
categories.js
const express = require('express')
const app = express()
var request = require('request');
var categoricalNames = []
var options = {
'method': 'GET',
'url': 'a working url',
'headers': {
'Authorization': 'working authorization'
},
'contentType': 'application/json'
};
var categoriesFunction = async() => request(options, function (error, response) {
return new Promise(function(resolve, reject){
console.log('inside categories function')
var categoryName = ''
if(error || !response)
{
// report error
reject(new Error('Try Again'));
}
else
{
//process response
var body = JSON.parse(response.body);
var jsonResponse = body.values
jsonResponse.forEach(function(obj) {
// console.log(obj.label)
categoryName = obj.label
// JSON.stringify(categoryName)
categoricalNames.push(categoryName)
});
categoricalNames.push({categoryName});
// console.log(categoricalNames)
// report success
JSON.stringify(categoricalNames)
resolve(categoricalNames);
}
})
});
module.exports.getPlaceDetails = categoriesFunction;
for awhile I thought my code wasn't working but console.logs in my categoriesFunction function reveal that the array is populating correctly. it's just not being pushed to the index correctly. and trying that method on the index.js page does not do anything for me. still just get [object Object]. not really sure why. Can anyone explain this to me?
Thanks!
I figured out what I needed to do. basically needed to write a cleaner Promise and make sure not to return the array but to resolve the array. and also there needed to be a variable in index.js that awaits the results of categoriesFunction()
here's what index.js looks like now
const express = require('express')
const router = new express.Router();
const categoriesFunction = require('../controllers/categories')
// get index page
router.get('', async (req, res) => {
let requestString = '/apps'
allApps = await openChannelRequest(requestString, req, res)
const thing = await categoriesFunction()
// this is important cause the method needs to be called before its inserted
await res.render('index', {
categories: thing,
// this is where I try to add the categories to the homepage
})
})
and here's what the categories.js file looks like
let categoriesFunction = function() {
return new Promise(resolve =>
request(options,
(error, response) => {
var categoryName = ''
var body = JSON.parse(response.body);
var jsonResponse = body.values
jsonResponse.forEach(function(obj) {
categoryName = obj.label
JSON.stringify(categoryName)
categoricalNames.push(categoryName)
});
console.log(categoricalNames)
resolve(categoricalNames)
// now that this is resolved it can be returned
}
))
}
As a part of an employee's app management, I want to separate the business logic database operations from my main application file.
The simplest operation is to read all the employees from the database using async/await to synchronize it:
module.exports.getEmployees = async () => {
const employees = await Employee.find();
return employees;
}
in my app.js I typed the following code:
const employee = require(__dirname + "/models/employee.js");
app.get("/employees", (req, res) => {
const employeeList = employee.getEmployees();
employeeList.then(res.send(employeeList));
})
but still, the array shows up empty?
then clause in promise accepts a functions as an argument & this function has a parameter which holds the actual response.
Something like this -
new Promise().then((response) => {
console.log(response);
});
You are doing employeeList.then(res.send(employeeList)); which means the argument to then clause is res.send() which won't work.
Try this -
employeeList.then((list) => {
// please note the response type here depends on the Content-Type set in the response Header
res.send(list);
// In case of normal http server, try this -
res.send(JSON.stringify(list));
});
I hope this helps.
I'm new to node and trying to understand how to use async call inside an other async function, here's an example.
My routing:
/**
* PAGE: PROFILE EXPOSITIONS
*/
exports.expositions = async(function* (req, res) {
respond(res, 'users/profile/exhibitors', ProfileResponse.expositions( req ));
});
Here's what ProfileResponse.expositions look like
exports.expositions = function*(req) {
return {
title: "Expositions",
user: req.user,
path: "/profile",
waiting_exposition_requests: yield UserService.getWaitingExpositionRequest(req.user.id)
};
};
I'm trying to use getWaitingExpositionRequest which looks like this
exports.getWaitingExpositionRequest = function*(userId) {
let currentUser = User.find(userId);
let waiting_exposition_requests = [];
currentUser.exposition_requests.forEach(function(exposition_request) {
if(exposition_request.status === "waiting_for_exhibitor") {
waiting_exposition_requests.push(exposition_request);
}
});
return waiting_exposition_requests;
};
But in the console I get a Promise error, any idea how I can use this async function returned? I'm sure it's a syntax error but I'm a little lost with the async stuff.