Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何修复未经处理的此错误PromisejectionWarning:TypeError[ERR\u unscaped\u CHARACTERS]:_Node.js_Express - Fatal编程技术网

Node.js 如何修复未经处理的此错误PromisejectionWarning:TypeError[ERR\u unscaped\u CHARACTERS]:

Node.js 如何修复未经处理的此错误PromisejectionWarning:TypeError[ERR\u unscaped\u CHARACTERS]:,node.js,express,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 (

我正在运行一个节点服务器,它从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)
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`)

})
我不太确定如何抓住错误

.catch(error => { 
  console.log(error.message)
});
尝试将此捕获添加到promise.all的then()末尾,然后查看它抛出了什么错误


现在,对于错误,问题可能是URL。因此,在分配变量URL1和URL2之后,请尝试此操作

URL1 = encodeURI(URL1);
URL2 = encodeURI(URL2);

我把鱼加进去了。我会继续监视。所以现在我在我的windows服务器上运行它进行开发测试。我通过巧克力安装了nod。我不太确定在哪里修改链接。非常感谢你的帮助。我将使用resultsAlright进行更新,以便控制台现在声明,请求路径包含未替换的字符。好奇这是什么要求。嗯,问题可能出在URL上。因此,在分配变量URL1和URL2,
URL1=encodeURI(URL1)
URL2=encodeURI(URL2)
,之后尝试此操作,