express icon indicating copy to clipboard operation
express copied to clipboard

Why does node not accept ${req.params} in url?

Open proach1995 opened this issue 1 year ago • 3 comments

I've come across the stranges bug I've ever seen. I'm writing a NodeJS v16 Backend and I request data from an CMS. I have used Axios and http Object in Node to do so, but both deliver the same Output. Requesting from variable doesWork gives me:

{
data: [ { id: 1, attributes: [Object] } ],
meta: { pagination: { page: 1, pageSize: 25, pageCount: 1, total: 1 } }
}

Requesting from doesNotWork gives me:

{
data: [ ],
meta: { pagination: { page: 1, pageSize: 25, pageCount: 1, total: 1 } }
}

app.get(`/blog/:URL_Identifier`, async (req, res) => {

let data; 
let doesWork = "http://127.0.0.1:1337/api/blogposts?filters[URL_Identifier][$in]      [0]=Blogpost1&populate=*"; 

console.log(req.params); // "Blogpost1"

let doesNotWork = http://127.0.0.1:1337/api/blogposts?filters[URL_Identifier][$in][0]=${req.params}&populate=*;
 
var options = { headers: { 'Authorization': 'Bearer ' + token     }   };

callback = function(response) { 
  var str = '' 
  response.on('data', function (chunk) { 
  str += chunk;     
  });

  response.on('end', function () { 
  console.log(str); 
  data = JSON.parse(str); 
  console.log(data);     
  });   
}
var req1 = await http.request(doesWork, options, callback); req1.end(); })

Does anybody have a hint on this? I do not have any glue about this and I'm trying to figure this out for days..

proach1995 avatar Oct 18 '22 11:10 proach1995