TypeError:无法读取属性' get'未定义-Node.js -URL参数

发布于 2025-02-13 20:14:22 字数 1454 浏览 2 评论 0原文

我正在尝试从我的客户端获取请求(来自用户输入)中获取某些参数,以获取API获取我在服务器上发送的请求。

使用。在我的search_params变量上使用。我正在使用'url'node.js软件包。

不确定为什么它是未定义的,我遵循本指南来解决我要解决的问题 - 1

下面的代码:(我评论了导致错误的两行,因此您更容易看到)

const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
    const search_params = full_url.searchParams;
    // const start = search_params.get('start')
    // const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);


    axios.get('https://'+bamboo_api_key+':[email protected]/api/gateway.php/velocitypartners/v1/time_off/whos_out/'+req.query.params, config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;

I am trying to get certain params from my client-side get request (which come from user input), to feed into an API get a request I send off on my server.

I get this error when using .get on my search_params variable. I am using the 'URL' node.js package.

not sure why it is undefined I followed this guide to work out a solution to what I am trying to solve - 1

Code below: (I commented out the two lines that are causing the error so it is easier for you to see)

const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
    const search_params = full_url.searchParams;
    // const start = search_params.get('start')
    // const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);


    axios.get('https://'+bamboo_api_key+':[email protected]/api/gateway.php/velocitypartners/v1/time_off/whos_out/'+req.query.params, config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2025-02-20 20:14:22

从我看到的full_url只是一个串联的字符串,显然没有方法/属性searchParams像对象一样。

因此,我们知道full_url.searchparams是未定义的。
这将评估

const search_params = undefined

哪些导致您的问题。

您使用调试器了吗?看到运行时的full_url看起来会很有帮助。尽管如此,这里应该是一个问题。

From what I see full_url is just a concatenated string which obviously has no method/attribute searchParams like an object would have.

So we know that full_url.searchParams is undefined.
This would evaluate to

const search_params = undefined

which results in your problem.

Did you use a debugger? It would be helpful to see what full_url looks like during run-time. Nonetheless, it being a string should be the problem here.

盛装女皇 2025-02-20 20:14:22

有一件小事,您需要知道并添加。 full_url是一个没有searchParams的字符串。因此,使其成为URL对象并使用其方法。



const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
   const myURL = new URL(full_url); // make it an URL object to use its methods
    const search_params = myURL.searchParams;
    const start = search_params.get('start')
    const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);



// you can use string literal here
  axios.get(`https://${bamboo_api_key}:[email protected]/api/gateway.php/velocitypartners/v1/time_off/whos_out/${req.query.params}`  , config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;

There is a small thing, you need to know and add. full_url is a string that doesn't have searchParams. So, make it a URL object and use their methods.



const axios = require('axios');
const router = require('express').Router();
const url = require('url');
require('dotenv').config();

const bamboo_api_key = process.env.BAMBOO_API_KEY;

const config = {
    headers: {
        'Accept':'application/json' 
    }
};

router.get('/bamboo/absencees', (req, res) => {

    const full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
   const myURL = new URL(full_url); // make it an URL object to use its methods
    const search_params = myURL.searchParams;
    const start = search_params.get('start')
    const end = search_params.get('end')

    console.log("full url: ", fullUrl);
    console.log(end);
    console.log(sart);



// you can use string literal here
  axios.get(`https://${bamboo_api_key}:[email protected]/api/gateway.php/velocitypartners/v1/time_off/whos_out/${req.query.params}`  , config)
    .then((response) => {
        console.log(response)
        console.log(req.query.params)
        res.send(response.data)
    })
    .catch(error => {
        console.log(error)
    });
})

module.exports = router;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文