-1

When I paste the endpoint URL with query directly inside the axios.get(), it responds correctly and I can see the json object returned. (i.e axios.get(http://localhost:3000/api/products/product_search?secretKey=${secret}&id=${blabla})). However, if I call the url with the summonerByNameUrl method, it crashes when I make a request. What is the problem in my code?

Crash report:

...
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]

Code: 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);
...
Murat
  • 19
  • 1
  • 5
  • What is `URL`? What does it return? If it's [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL), then you should be calling its constructor, ie `new URL()`. – Phil Nov 11 '21 at 22:09
  • URL is: https://xxx.herokuapp.com/ The request returns a json object. I tried calling new URL() but it says it is already a URL object. I am passing an URL into the getSummonerByName() – Murat Nov 11 '21 at 22:16
  • You misunderstood my comment. Your code literally has `URL(hidden)` where neither `URL` nor `hidden` are defined in your question. I am asking what those are and what they do – Phil Nov 11 '21 at 22:24
  • Oh, sorry for misunderstanding. I hid the URL myself. Thats why I wrote **URL(hidden)**. It is actually a URL in the code like: xxx.herokuapp.com – Murat Nov 11 '21 at 22:24
  • 2
    Try not to make obfuscation look like actual code, it gets very confusing – Phil Nov 11 '21 at 22:25

2 Answers2

0

You're calling your function with getSummonerByName(req.query) where it is clear from the lines just before that req.query is an object and not a string. When objects are used in a string-context (like your URL), they become "[object Object]", hence the error.

Taking some guesses here but it seems you want to forward some req.query information to the Axios call as query params. Try this instead...

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
}

If you've got a helper function that returns the base URL (ie http://localhost:3000/api/products/product_search) then by all means, use that instead of a string literal in the Axios call.

Phil
  • 157,677
  • 23
  • 242
  • 245
-1

The req.query is a Object, not a string.

You can try map the req.query object to make a string. Something like that:

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

This code return a string like that: 'id=1&name=test', so you can pass to the endpoint.

  • 2
    This will produce invalid URLs if the query contains special characters. You forgot encoding the keys and values. However, an even simpler way (which also handles encoding correctly) would be `new URLSearchParams(Object.entries(req.query)).toString()` instead of that whole code. – CherryDT Nov 11 '21 at 22:55
  • 2
    @CherryDT or since OP is using Axios already, just pass in a `params` object – Phil Nov 11 '21 at 23:44