Reputation: 13
I'm running a node server that pulls data from an api. I get this error randomly which I'm assuming is from either a character that's not being handled correctly.
the error:
(node:7988) UnhandledPromiseRejectionWarning: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
at new ClientRequest (_http_client.js:140:13)
at Object.request (https.js:309:10)
at RedirectableRequest._performRequest (C:\api\finder\node_modules\follow-redirects\index.js:169:24)
at new RedirectableRequest (C:\api\finder\node_modules\follow-redirects\index.js:66:8)
at Object.wrappedProtocol.request (C:\api\finder\node_modules\follow-redirects\index.js:307:14) at dispatchHttpRequest (C:\api\finder\node_modules\axios\lib\adapters\http.js:180:25)
at new Promise (<anonymous>)
at httpAdapter (C:\api\finder\node_modules\axios\lib\adapters\http.js:20:10)
at dispatchRequest (C:\api\finder\node_modules\axios\lib\core\dispatchRequest.js:59:10)
at async Promise.all (index 0)
(node:7988) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 30)
The code in question from my app.js:
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const port = process.env.PORT || 80
const axios = require('axios')
const fetch = require('node-fetch')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(__dirname + '/public'))
app.get('/', function(req, res) {
if (req.query.keyword === undefined) {
return res.render('home')
}
let keyword = req.query.keyword.toUpperCase()
keyword = keyword.charAt(0).toUpperCase() + keyword.slice(1).toLowerCase()
let keywordForAdinterest = keyword.replace(/'/g, "");
let keywordForAdinterestsuggestion = keyword.replace(/'/g, "");
let URL1 = `https://graph.facebook.com/search?type=adinterest&q=[%27${keywordForAdinterest}%27]&limit=10500&locale=en_US&access_token=${process.env.ACCESS_TOKEN}`
let URL2 = `https://graph.facebook.com/search?type=adinterestsuggestion&interest_list=['${keywordForAdinterestsuggestion}']&limit=10500&locale=en_US&access_token=${process.env.ACCESS_TOKEN}`
let promise1 = axios.get(URL1)
let promise2 = axios.get(URL2)
Promise.all([promise1, promise2]).then(function(values) {
const adinterest = values[0].data
const adinterestsuggestion = values[1].data
res.render('home', { keyword, adinterest, adinterestsuggestion, })
})
})
app.listen(port, () => {
console.log(`Express is running on ${port} visit http://localhost:80`)
})
I'm not too sure how to .catch the error.
Upvotes: 1
Views: 2770
Reputation: 440
.catch(error => {
console.log(error.message)
});
Try adding this catch at the end of the then() for promise.all and see what error it throws.
Now for the error, The issue might be with the URL. So try this after assigning the variable URL1 and URL2,
URL1 = encodeURI(URL1);
URL2 = encodeURI(URL2);
Upvotes: 2