如何解决此错误UnhandledPromiseRejectionWarning:TypeError [ERR_UNESCAPED_CHARACTERS]:

时间:2019-08-28 07:21:19

标签: node.js express

我正在运行一个从api提取数据的节点服务器。我随机得到此错误,我认为这是由于字符处理不正确造成的。

错误:

(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)

我的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`)

})

我不太确定如何捕获该错误。

1 个答案:

答案 0 :(得分:0)

"COLUMNS"

尝试在 promise.all 的then()末尾添加此捕获,并查看它会引发什么错误。

现在出现错误,问题可能出在URL。因此,在分配变量URL1和URL2之后尝试

.catch(error => { 
  console.log(error.message)
});