【问题标题】:node.js:Can't set headers after they are sentnode.js:发送后无法设置标头
【发布时间】:2018-03-13 07:18:26
【问题描述】:

我现在正在学习 Node.js,我正在尝试创建一个购物清单应用程序,并且我正在尝试实现一个搜索路由,它检查查询是否与 val 匹配,这是代码

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

//Array List 
let list = ['Fish', 'Lettuce', 'Chicken'];

//Set view engine to pug
app.set('view engine', 'pug');
//use bodyParser


app.get('/', function(request, response){
    response.render('list', {list});
});

app.get('/search', function(request, response){
   return list.map(function(val){
      if(request.query.search === val){
          return response.send('Yup you got ' + val);
        }
     response.send('Not found')
    });
});

app.get('/new-item', function(request, response){
    response.render('new');
});

app.post('/add-item', function(request, response){
   let add = response.send(request.body);
   list.push(add);
});

app.listen(port, function(){
    console.log('Listening on Port 3000');
});

现在问题出在/search 路由的if 条件下,我知道我收到错误的原因是因为我不能使用response.send 两次,我正在寻找一种发送任一响应的方法,基于是否满足条件。 任何帮助表示赞赏 谢谢

【问题讨论】:

  • 如果条件失败,请使用“else”。
  • 尝试将第二个条件放在else中
  • 我确实把它放在了一个 else 块中,但仍然是 samw

标签: javascript node.js server


【解决方案1】:

在这段代码中:

app.get('/search', function(request, response){
   return list.map(function(val){
      if(request.query.search === val){
          return response.send('Yup you got ' + val);
        }
        response.send('Not found')
    });
});

您在.map() 回调中执行response.send(),这意味着您可以轻松地多次调用它,并且您询问的错误表明您多次调用它。请记住,.map() 中的return 不会脱离.map()。它仅从回调函数的该迭代返回,然后 .map() 的下一次迭代在您 return 之后继续。

如果您想跳出迭代,则切换到常规的 for 循环进行迭代(不使用回调),然后您的 return 将执行您想要的操作,如下所示:

app.get('/search', function(request, response){
    for (let val of list) {
        if (request.query.search === val){
            return response.send('Yup you got ' + val);
        }
    }
    response.send('Not found')
});

【讨论】:

  • 此代码不起作用。在第二次迭代没有找到元素的情况下,会抛出错误。
  • 嘿@jfriend00 你能帮我吗stackoverflow.com/questions/46484088/…
  • @ogbeh - 当你完全描述了当list 中有多个项目并且一些匹配而一些不匹配时你期望的行为,然后只有这样我才会花时间编辑我的匹配该描述的代码。您目前没有说出所需的行为是什么。无法编写代码来匹配不存在的规范。你现在的问题还不清楚。也许其他一些答案正确地猜到了你想要的,但好问题不需要猜测 - 它们拼写出所需行为的确切细节。
  • @alexmac - OP 根本不清楚他们期望的行为是什么。对你有好处,你猜对了,但好问题不需要猜测。
【解决方案2】:

response.send('Not found') 移出循环。另外,你不应该在这里使用Array.map,而是使用Array#find

app.get('/search', function(request, response) {
   let foundVal = list.find(function(val) {
     if (request.query.search === val) {
       return val;
     }
   });
   if (foundVal) {
     return response.send('Yup you got: ' + foundVal);
   }
   response.send('Not found');
});

【讨论】:

【解决方案3】:

使用回调构建您的结构。

app.get('/search', function(request, response){
    checkValue(list,request.query.search,function (result) {
        response.send({
            data : result
        });
    });

    function checkValue(list, value, callback) {
        var isHere = false;
        list.map(function(val){
            if(request.query.search === val){
                isHere = true;
            }
        });
        callback(isHere);
    }
});

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 2018-05-05
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多