You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用axios.get()时报Cannot GET /[object%20Object]错误如何排查?

问题根因

你看到的[object%20Object]是JavaScript将对象强制转为字符串的默认结果,本质是传参类型不匹配+URL拼接逻辑缺失,具体有两个核心错误:

  • 第一个错误:summonerParser 中调用 getSummonerByName 时传了整个 req.query 对象,而不是需要的summonerName字符串参数。getSummonerByName接收到对象后传入summonerByNameUrl做字符串拼接,自然就把对象转成了[object Object],拼到URL里就变成了你报错里的/[object%20Object]。
  • 第二个错误:当前的summonerByNameUrl拼接逻辑只加了summonerName,没有携带接口必须的secretKey查询参数,就算修了第一个传参问题,请求也会因为缺少密钥被拒绝。
修复方案

1. 修改config.js的URL拼接逻辑

补充查询参数拼接,同时对参数做URL编码避免特殊字符导致请求异常:

// 替换为你的实际目标接口基础路径
const BASE_URL = 'http://你要请求的第三方接口基础地址'
const summonerByNameUrl = (summonerName, secretKey) => `${BASE_URL}?secretKey=${secretKey}&summonerName=${encodeURIComponent(summonerName)}`;

module.exports = {
    summonerByNameUrl
}

2. 修改summoner.js的传参逻辑

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

// 新增接收secretKey参数
const getSummonerByName = async (summonerName, secretKey) => {
    const res = await axios.get(config.summonerByNameUrl(summonerName, secretKey));
    return res.data;
}

const summonerParser = async (req, res) => {
    if(!req.query.secretKey || !req.query.summonerName)
        return res.status(400).json({error: '缺少必填参数:secretKey或summonerName'})
    // 从req.query中取出对应字段传参,不要传整个req.query对象
    // 如果你的召唤师名称参数名不是summonerName,替换成实际的参数名即可
    let data = await getSummonerByName(req.query.summonerName, req.query.secretKey)
    return res.status(200).json(data);
}

module.exports = {
    getSummonerByName,
    summonerParser
}

内容的提问来源于stack exchange,提问作者Murat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 21:45:04