0

I'm trying to send a contactItem (string element) from my index.html to my js application using angular.js via http.post. Every time I try to post the contactItem I get the following error:

SyntaxError: Unexpected token m (or D)

at parse.

I tried JSON.stringify(contactItem) and JSON(contactItem);, but it didn't help.

Hopefully someone can help me figure this out. Thanks.

enter image description here

index.html

<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 
<table style="width:100%">
 <tr ng-repeat="contact in databases.databases">
    <td>{{ contact.name }} <button type="button"ng-click="addContactlist(contact.name)">Click Me</button></td>
    <td>{{ contact.sizeOnDisk }} </td>
    <td>{{ contact.empty }} </td>
  </tr>
  </table> 
</div>

</body>

global.js

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope, $http) {
    console.log("controller connected");

function refresh(){ 
// create contacklist route 
$http.get('/databases').success(function(response) {
    console.log("recived data requested");
    $scope.databases = response; 
  });
}

// Call refresh to init cantacklist 
refresh(); 


// add Doc to table 
$scope.addContactlist = function(contactItem) {
  alert("Working ");
    $http.post('/collection', JSON.stringify(contactItem)).success(function(response) {
      });
    console.log("posted: "+contactItem);
  };


});// Controller 

mvc.js

var express = require('express');
var path = require('path'); //core module 
var databaseUrl = "localhost:27017/DB"; // default env
var bodyParser = require('body-parser');

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    assert = require('assert');

//configure app
var app = express(); 
var db = new Db('DB', new Server('localhost', 27017));


db.on('error', function (err) {
    console.log('database error', err)
}); 

db.on('connect', function () {
    console.log('database connected')
}); 

// store all html files in views 
app.use(express.static(__dirname + '/views'));
// parses recived json input 
app.use(bodyParser.json());
// store all js in Scripts folder
app.use(express.static(__dirname + '/scripts'));

// Technology not needed but good practice, especailly when serval people are working on it 
app.get('/', function (req, res) {
    res.sendFile('index.html');
}); 

// listen for contactlist get request, aka transfers the contacklist in mongo to client
app.get('/databases', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {

      // Use the admin database for the operation
      var adminDb = db.admin();

      // List all the available databases
      adminDb.listDatabases(function(err, dbs) {
        assert.equal(null, err);
        assert.ok(dbs.databases.length > 0);
        console.log(dbs);
        res.json(dbs); 
        db.close();
      });
    });
}); 

// listen for contactlist get request, aka transfers the contacklist in mongo to client
app.get('/collection', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {
         // Grab a collection without a callback no safe mode
        var col1 = db.collection('DB');
    });
}); 



// Implement a web server to listen to requests 
app.listen(4444, function(){
    console.log('ready on port 4444'); 
}); 

1 Answer 1

1

$http.post need data should be passed in json format like here contactItem is nothing only contact.name which wouldn't get strigify in any sense. I would suggest you to create JSON object before parsing it.

$http.post('/collection', JSON.stringify({'contactItem': contactItem})
//$http.post('url', data); //data should be in json format.

Also on node.js code that call should be app.post instead of app.get

Sign up to request clarification or add additional context in comments.

9 Comments

This fixed the parse error. However app.get(), isnt printing to the console. Should i not use stringify() ?
Yes..try without stringify please
SyntaxError: Unexpected token D at parse. Yeah idk what else to try
Is the problem the routing ?
I think the post call should be app.post instead of app.get
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.