【问题标题】:Cannot GET /[object%20Object] when calling axios.get()调用 axios.get() 时无法获取 /[object%20Object]
【发布时间】:2021-12-24 08:41:04
【问题描述】:

当我将带有查询的端点 URL 直接粘贴到 axios.get() 中时,它会正确响应并且我可以看到返回的 json 对象。 (即 axios.get(http://localhost:3000/api/products/product_search?secretKey=${secret}&id=${blabla}))。但是,如果我使用 SummonerByNameUrl 方法调用 url,它会在我发出请求时崩溃。我的代码有什么问题?

崩溃报告:

...
data: '<!DOCTYPE html>\n' +
      '<html lang="en">\n' +
      '<head>\n' +
      '<meta charset="utf-8">\n' +
      '<title>Error</title>\n' +
      '</head>\n' +
      '<body>\n' +
      '<pre>Cannot GET /[object%20Object]</pre>\n' +
      '</body>\n' +
      '</html>\n'
  },
  isAxiosError: true,
  toJSON: [Function: toJSON]

代码: config.js

const summonerByNameUrl = (summonerName) => `${URL(hidden)}${summonerName}`;

module.exports = {
    summonerByNameUrl
}

summoner.js

const config = require('../config');
const axios = require('axios');

const getSummonerByName = async (summonerName) => {
    const res = await axios.get(config.summonerByNameUrl(summonerName));
    return res.data;
}

const summonerParser = async (req, res) => {
    if(!req.query.secretKey)
        return res.status(403).json({error: 'missing secret key.'})
    let data = await getSummonerByName(req.query)
    return res.status(200).json(data);
}

module.exports = {
    getSummonerByName,
    summonerParser
}

products.js

var express = require('express');
var axios = require('axios')
var router = express.Router();

const summoner = require('../services/summoner');

router.get('/product_search', summoner.summonerParser)
module.exports = router;

app.js

...
app.use('/api/products', productsRouter);
...

【问题讨论】:

  • 什么是URL?它返回什么?如果是URL,那么你应该调用它的构造函数,即new URL()。
  • 网址为:xxx.herokuapp.com 请求返回一个json对象。我尝试调用 new URL() 但它说它已经是一个 URL 对象。我将一个 URL 传递到 getSummonerByName()
  • 你误解了我的评论。您的代码实际上具有 URL(hidden) ,而您的问题中既没有定义 URL 也没有定义 hidden 。我在问那些是什么以及它们做什么
  • 哦,误会了。我自己隐藏了网址。这就是我写 URL(hidden) 的原因。它实际上是代码中的一个 URL,如:xxx.herokuapp.com
  • 尽量不要让混淆看起来像实际代码,它会变得非常混乱

标签: node.js axios get get-request


【解决方案1】:

您正在使用getSummonerByName(req.query) 调用您的函数,从前面的行可以清楚地看出req.query 是一个对象而不是字符串。当在字符串上下文中使用对象时(例如您的 URL),它们会变成“[object Object]”,因此会出现错误。

在这里进行一些猜测,但您似乎想将一些 req.query 信息作为查询参数转发给 Axios 调用。试试这个吧……

const PRODUCT_SEARCH_URL = "http://localhost:3000/api/products/product_search"

const getSummonerByName = async ({ secretKey, id }) => {
  const { data } = await axios.get(PRODUCT_SEARCH_URL, {
    params: { secretKey, id }
  })

  return data
}

如果您有一个返回基本 URL 的辅助函数(即http://localhost:3000/api/products/product_search),那么请务必在 Axios 调用中使用它而不是字符串文字。

【讨论】:

    【解决方案2】:

    req.query 是一个对象,而不是一个字符串。

    您可以尝试将 req.query 对象映射为字符串。类似的东西:

    Object.keys(req.query).map(key => {
       return key + '=' + req.query[key]
    }).join('&')
    

    此代码返回一个类似这样的字符串:'id=1&amp;name=test',因此您可以传递给端点。

    【讨论】:

    • 如果查询包含特殊字符,这将产生无效的 URL。您忘记了对键和值进行编码。但是,更简单的方法(也可以正确处理编码)是new URLSearchParams(Object.entries(req.query)).toString(),而不是整个代码。
    • @CherryDT 或者由于 OP 已经在使用 Axios,只需传入一个 params 对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多