I am new to building applications using MEAN stack and am trying to build a website that queries a mongodb data collection that I have saved. I managed to retrieve data through the get method within my server.js file, but am struggling to query the data using the post method.
Within my html index file I have implemented a search bar:
<form class="navbar-form navbar-right" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term" ng-model="keywords">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search" ng-click="mongoQuery()"></i></button>
</div>
</div>
</form>
My controller.js file then initiates the call to the server using the post method:
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
// GET function
$http.get('/search_results').then(successCallback, errorCallback);
function successCallback(response){
console.log("I got the data I requested");
$scope.programmeList = response;
}
function errorCallback(error){
console.log("I DID NOT got the data I requested");
}
// POST function
$scope.mongoQuery = function(){
console.log($scope.keywords);
$http.post('/search_results', $scope.keywords);
}
}]);
I then attempt to retrieve all records that match the keyword entered in the search bar in my server.js file as such:
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('mydb', ['bbctest']);
var bodyParser = require('body-parser');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
// this function works
app.get('/search_results', function(req, res){
console.log("I recieved a GET request")
db.bbctest.find().sort({epoch_start: -1},function (err, docs) {
console.log(docs);
res.json(docs);
});
});
// THIS FUNCTION DOES NOT WORK
app.post('/search_results', function(req, res){
//console.log(req.body);
db.bbctest.find( { $or: [ { categories:$scope.keywords} , {tags:$scope.keywords } ] }, { complete_title: 1 } ).forEach(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.listen(3000);
console.log("Server running on port 3000");
After testing my search bar, the terminal where I am running the node server prints the following error:
SyntaxError: Unexpected token h
at parse (/Users/ss/Documents/Back_End_Projects/node_modules/body-parser/lib/types/json.js:83:15)
at /Users/ss/Documents/Back_End_Projects/node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/Users/ss/Documents/Back_End_Projects/node_modules/raw-body/index.js:262:16)
at done (/Users/ss/Documents/Back_End_Projects/node_modules/raw-body/index.js:251:7)
at IncomingMessage.onEnd (/Users/ss/Documents/Back_End_Projects/node_modules/raw-body/index.js:307:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Also checking the console I find that I have this angularjs error:
angular.js:12587 POST http://localhost:3000/search_results 400 (Bad Request)
And this one appears in the web console also:
Possibly unhandled rejection: {"data":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>SyntaxError: Unexpected token h<br> at parse
I have been trying to figure out what I am doing wrong for a few days now but have not gotten to bottom of it. I would appreciate any help at this point. Also is there are any useful resources out there that you guys would recommend that would help greatly.
Thanks very much.
via sums22
No comments:
Post a Comment