Itsik Mauyhas
Itsik Mauyhas

Reputation: 3984

connect mongo to node error

Im tring to connect node server to mongo and there is my code :

var http = require("http");
var url = require("url");
var Router = require('node-simple-router');
var router = Router();
var qs = require('querystring');
var mongoose = require('mongoose');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// mongo connection and use 
mongoose.connect('mongodb://localhost/jobs');
var db = mongoose.connection;
db.on('error', function() {console.log("error")});
db.once('open', function () {
console.log("connected!");
// init the schema 
var jobSchema = mongoose.Schema({ bName: String , phone :Number ,location:[{longitude:Number,latitude:Number}],Email:String,field:String,exp:String});
jobSchema.methods.printDetails = function() {
 var str = "id=" + this.id + " name="+this.name;
 console.log(str);
 };
var job = mongoose.model('jobs',jobSchema);
 //adding a jobs for testing : 
 var testJob = new job ({bName: 'Microsoft' , phone :'035588319' ,location:[{longitude:'7.8',latitude:'7.8'}],Email:'[email protected]' ,field:'QA',exp:'0-2'});
    testJob.save(function(error,prod) {
    if(error) {
    console.log(error);
            }
    else {
        console.log("a job was saved to mongodb");
        //testJob.printDetails();
        }
)};


function start(route) {
  function onRequest(request, response) {
    var path =  url.parse(request.url).pathname;
    console.log("Request for " +path+ "received.");
    route(path);
    if(request.method==='GET')
    {

        response.writeHead(200, {"Content-Type": "text/plain","access-control-allow-origin":"*"});
        console.log(request.body);
        response.end("res");    

    }
     else if(request.method === 'POST')
    {

        response.writeHead(200, {"Content-Type": "text/plain","access-control-allow-origin":"*"});
        //var userString = JSON.stringify(body);

        response.end("POST REQUEST");

    }
    else if (request.method==='PUT')
    {
        response.writeHead(200, {"Content-Type": "text/plain","access-control-allow-origin":"*"});
        response.end("put request");
    }
    else if(request.method==='DELETE')
    {
        response.writeHead(200, {"Content-Type": "text/plain","access-control-allow-origin":"*"});
        response.end("delete request");
    }

  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

i dont start my server because of syntax error on line 79 , but i only have 78 lines . syntax error unexpected end of input : }); , and can anyone tell me if this is the right way to connect between the mongo and the node web server .

Upvotes: 0

Views: 110

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

When you call db.once("open", function () { you never close that paren and brace.

I think you want a }); after the testJob declaration. That should also be }) and not )}.

Upvotes: 1

Related Questions