在Express API端点上获取重复结果。如何重置搜索,以便每次都能获得新结果?

问题描述 投票:0回答:1

我正在使用Express创建端点,以便可以通过API调用来访问它。第一次进行搜索时,一切正常,但是如果再次执行此操作,则可以获取上一次的结果以及新搜索的结果。如何使搜索结果每次重设?

这里是到实际端点的链接:(将您喜欢的任何搜索词改成“ covid”一词,如果您至少进行两次,即使执行新搜索后,您仍会看到来自上一次搜索的数据)

https://laffy.herokuapp.com/search/covid

非常感谢您提供的任何帮助!

这是一个调用twitterRouter并与app.use一起使用的app.js文件,它在/ search /:searchTerm:处创建端点。

app.js

const createError = require('http-errors');
const express = require('express');
const path = require('path');
const indexRouter = require('./routes/index');
const twitterRouter = require('./routes/twitterCall.js');
const top20 = require('./routes/twitterTop20.js');
const app = express();

app.set('views', path.join(__dirname, 'views'));
// app.set('port', process.env.PORT || 3001);

app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);

//creates route to use search at /search/
app.use('/search/:searchTerm', twitterRouter.search);
//creates route to access to get the top 20 Twitter hashtags trending
app.use('/top20', top20); 

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

我给人的印象是,使用res.send()结束了API搜索,但似乎没有结束。

然后是实际的API调用,并在其中生成端点的数据:

twitterCall.js

//twitter file that searchs for tweets specified in params.q

var Twitter = require('twitter');
var config = require('../config/config.js');
var express = require('express');
var router = express.Router();


var T = new Twitter(config);
var locationsToSend = [];

exports.search = (req, res) => {
    if (req.body == null) {
        res.status(404).send( {
            message: "Search can not be blank"
        })
    }
    var params = {
        q: req.params.searchTerm,
        count: 1000,
        result_type: 'recent',
        lang: 'en'
    }


//Initiate your search using the above parameters
T.get('search/tweets', params, function(err, data, response) {
    //if there is no error, proceed
  if(!err){
   // Loop through the returned tweets
    for(let i = 0; i < data.statuses.length; i++){


      if (data.statuses[i].user.location!==null && data.statuses[i].user.location!=="") {
        locationsToSend.push({
          id: data.statuses[i].id_str, 
          createdAt: data.statuses[i].created_at,
          text: data.statuses[i].text,
          name: data.statuses[i].user.screen_name,
          location: data.statuses[i].user.location
        });
      }

    }
    res.send(locationsToSend);

  } else {
    console.log(err);
    return res.status(404).send({
                message: "error searching " + err
            });


  }
});


};

我正在使用Express创建端点,以便可以通过API调用来访问它。第一次进行搜索时,一切正常,但是如果再次进行搜索,则会从前一个搜索结果中获得结果...

node.js express endpoint
1个回答
0
投票

您的locationsToSend变量在全局范围内,只要您的Express应用程序正在运行,该变量就会保留。您应该在search/tweets回调中初始化该变量,然后将获得所需的行为。这样,每个请求将获得自己的locationsToSend来处理,而不是全局请求。

© www.soinside.com 2019 - 2024. All rights reserved.