I've got this api route setup and it returns data from my staff.json file if I manually type in /api into the browser:
var express = require('express');
var router = express.Router();
var staffData = require('../data/staff.json')
router.get('/api', function(req, res){
res.json(staffData);
});
module.exports = router;
However when I try to make it work in one of my views using this function:
$('#search').click(function(){
$.getJSON('api', function(key,val){
var search = $('#search').val();
var regex = new RegExp('search', 'i');
var output;
$.each(staffData, function(key, val){
if(val.name.search(regex) != -1 || val.email.search(regex)){
output += "<tr>";
output += "<td id='"+key+"'>"+val.name+"</td>";
output += "<td id='"+key+"'>"+val.email+"</td>";
output += "</tr>";
}
});
$('tbody').html(output);
console.log(output);
});
});
I keep getting an error saying staffData is not defined, although it has clearly been defined and returned in my api route.
Can anyone explain what the problem may be?
Regards
</div>