0

I am learning Node.js at the moment, and i am trying to create a shopping list application, and i am trying to implement a search route, which checks if the query matches the val, here is the code

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'); }); 

Now the problem is with the /search route's if conditional, i know the reason i am getting the error is because i can't use the response.send twice, i am looking for a way to send either response, based on if the condition is met. Any help is appreciated Thanks

3
  • Use "else" if your condition gets failed. Commented Oct 2, 2017 at 8:15
  • try putting the second condition in else Commented Oct 2, 2017 at 8:15
  • i actually did put it in an else block, but still the samw Commented Oct 2, 2017 at 8:19

3 Answers 3

1

Move response.send('Not found') outside of the loop. Also, you shouldn't use Array.map here, use Array#find instead:

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'); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

hey @alexmac can you please help me stackoverflow.com/questions/46484088/…
1

Build your structure with callbacks.

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); } }); 

Comments

0

In this piece of code:

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') }); }); 

You are doing response.send() inside your .map() callback which means you can easily call it more than once and the error you asked about indicates that you are calling it more than once. Keep in mind that the return inside your .map() does not break out of the .map(). It only returns from that iteration of the callback function and then the next iteration of the .map() continues right after you return.

If you want to break out of the iteration, then switch to a regular for loop to do your iteration (which does not use a callback) and then your return will do what you want like this:

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') }); 

4 Comments

This code won't work. In the case when the element is not found at the second iteration, the error will be thrown.
hey @jfriend00 can you please help me stackoverflow.com/questions/46484088/…
@ogbeh - When you fully describe what you expect the behavior to be when there are multiple items in list and some match and some do not, then and only then will I spend time on editing my code to match that description. You do not currently say what the desired behavior is. Can't write code to match a specification that does not exist. Your question as it stands now is unclear. Maybe some other answers correctly guessed what you wanted, but good questions do NOT require guessing - they spell out the exact details of the desired behavior.
@alexmac - The OP is not clear at all what they expect the desired behavior to be. Good for you that you guessed appropriately, but good questions do not require guessing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.