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

Getting duplicate results on Express API endpoint. How do I reset the search so that I get new results each time?

我正在使用 Express 创建端点,以便我可以通过 API 调用访问它。当我第一次进行搜索时,一切都很好,但如果我再次进行搜索,我会得到上一次的结果加上新搜索的结果。如何让搜索结果每次都重置?

这是指向实际端点的 link:(为您喜欢的任何搜索词更改 "covid" 一词,如果您至少执行两次,您将看到上次搜索的数据即使在您进行新搜索后也会显示)

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

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

这是 app.js 文件,它调用 twitterRouter 并使用 app.use 在 /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
            });


  }
});


};

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