是否可以将来自POST请求的数据存储到变量中?

发布于 2025-01-21 18:00:30 字数 1981 浏览 1 评论 0原文

我提出了一个简单的帖子请求,将搜索栏中的数据获取到后端。我能够安装它 - log-但是我需要能够存储该信息(作为变量/const),如果它符合条件,我需要能够将其添加到后端的函数中并返回基于该功能的数据。这是可以使用PUT请求的,还是我应该使用query.params?

app.post("/search", (req, res) => {
    const result = req.body;
    res.send({
        search: result,
    });
});
  1. 将数据从搜索栏中拉到后端,
  2. 使用搜索的关键字
  3. 返回基于关键字的

供参考的新信息运行一个函数 - 这是我想输入搜索的关键字的函数,以适用于 searchbarresulthere 的每个实例(( ,其中有7个),

async function **searchBarResultHere**() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/**searchBarResultHere**/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();


    await connectToMongoDB();
     // fetch stock/crypto data

    const listingModel = new **searchBarResultHere**({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });
    
    // save into MongoDB Modal
    await listingModel.save();

    // UPDATE stock price if it changed
    await Stock.findOneAndUpdate(
        {
            symbol: **searchBarResultHere**,
        }, 
        {
        price: Price, 
        symbol: **searchBarResultHere**, 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );

    
    
        console.log(Price)
    setTimeout(**searchBarResultHere**, 1000000); // 60 seconds == 1minute // not needed with node-cron


    
   
}

**searchBarResultHere**();

简而言之 这是一个在Yahoo上进行的程序。金钱并定期记录Yahoo.finance网站的一些选择的文本字段..然后是Store&用库存/加密名称,价格和24小时更改将其显示回HTML,

我想让人们输入股票名称(或符号),并使程序为他们获取所有数据,以便他们可以看到它而无需去看在Yahoo Finance上:)

但是我正在努力弄清楚如何在股票名称和将搜索结果发送到后端功能之间建立连接,以从yahoo.finance获取所有数据。

I made a simple POST request to fetch data from a search bar into backend. I am able to console.log it - but I need to be able to store that information (as a variable/const ideally) and IF it fits the criteria, I need to be able to add it into a function in the backend and return data based on that function. Is this possible with a PUT request or should I use query.params ?

app.post("/search", (req, res) => {
    const result = req.body;
    res.send({
        search: result,
    });
});
  1. Pull data from search bar into backend
  2. Run a function using the searched keyword
  3. Return new information based around the keyword

for reference - here's the function that I would like to enter the searched keyword into for every instance of searchBarResultHere ( and there's 7 of them )

async function **searchBarResultHere**() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/**searchBarResultHere**/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();


    await connectToMongoDB();
     // fetch stock/crypto data

    const listingModel = new **searchBarResultHere**({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });
    
    // save into MongoDB Modal
    await listingModel.save();

    // UPDATE stock price if it changed
    await Stock.findOneAndUpdate(
        {
            symbol: **searchBarResultHere**,
        }, 
        {
        price: Price, 
        symbol: **searchBarResultHere**, 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );

    
    
        console.log(Price)
    setTimeout(**searchBarResultHere**, 1000000); // 60 seconds == 1minute // not needed with node-cron


    
   
}

**searchBarResultHere**();

in a nutshell this is a program that goes on yahoo.finance and periodically records some chosen text fields from the yahoo.finance website.. then stores & displays it back into HTML with Stock/Crypto Name, Price and 24 hour change

I want to let people type in a stock name (or symbol), and make the program fetch all the data for them so they can see it without having to go on yahoo finance :)

But I'm struggling to figure out how to make the connection between seaching for a stock name and sending the search result to the backend function to fetch all data from yahoo.finance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文