-1

I have a basic express project created. And I've created a file called lib/userhandler.js inside root folder.

//lib/userhandler.js
exports.addUser = function(req, res){
  // Set our internal DB variable
  var db = req.db;

  // Get our form values. These rely on the "name" attributes
  var uName = req.body.username;
  var uEmail = req.body.useremail;

  // Set our collection
  var collection = db.get('usercollection');

  // Submit to the DB
  collection.insert({
    "username" : uName,
    "email" : uEmail
  }, function (err, doc) {
    if (err) {
      // If it failed, return error
      res.send("There was a problem adding the information to the database.");
    }
    else {
      // If it worked, set the header so the address bar doesn't still say /adduser
      //res.location("userlist");
      // And forward to success page
      res.redirect("userlist");
    }
  });
}

In my routs/users.js file, whenever the users page is loaded I want to send name and the mail values throught userhandler.js to the database.

//routes/users.js
var express = require('express');
var router = express.Router();
var User = require("../node_modules/SimpleExpress/routes/userhandler.js");

var name = "testuser6";
var mail = "testuser6@testdomain.com";

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
  User.addUser(name, mail);
});

module.exports = router;

When I try to load users page it shows "Can't set headers after they are sent."

Thank You

user3280033
  • 11
  • 1
  • 5
  • I omitted res.send and now it says "Cannot read property 'get' of undefined". I have to mention this. I am trying to create a simple framework to insert and get data from db. – user3280033 Jul 13 '15 at 12:59

2 Answers2

0

You should try to return the error from your db to your route handler through a callback like this :

//routes/users.js
var express = require('express');
var router = express.Router();
var User = require("../node_modules/SimpleExpress/routes/userhandler.js");

var name = "testuser6";
var mail = "testuser6@testdomain.com";

/* GET users listing. */
router.get('/', function(req, res, next) {
  User.addUser(name, mail, function(err, doc) {
    if(err) {
      res.send("There was a problem adding the information to the database.");
    } else {
      res.redirect("userlist");
    }
  });
});


//lib/userhandler.js
exports.addUser = function(name, mail, cb){
  // Set our internal DB variable
  var db = req.db;

  // Set our collection
  var collection = db.get('usercollection');

  // Submit to the DB
  collection.insert({
    "username" : name,
    "email" : mail
  }, function (err, doc) {
    cb(err, doc);
  });
}
Sachacr
  • 704
  • 1
  • 9
  • 17
0

You shouldn't insert the request and response objects as parameters of the function addUser(). They should be in the router callback function. I added a new parameter to the function, so that you can pass the database as a parameter thanks to the router which receives the request object as a parameter.

//lib/userhandler.js
exports.addUser = function(uName, uEmail, db){

  var collection = db.get('usercollection');
  var result = true;
  
  collection.insert({
    "username" : uName,
    "email" : uEmail
  }, function (err) {
    if (err) {
      result = false;
    }
  });
  
  return result; // true or false
}

I changed the code here also, so that the name and email variables can be received from the req and res parameters.

//routes/users.js
var express = require('express');
var router = express.Router();
var User = require("../node_modules/SimpleExpress/routes/userhandler.js");

//var name = "testuser6"; // I don't think you need this
//var mail = "testuser6@testdomain.com"; // and this

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
  
  var db = req.db;
  var name = req.body.username;
  var mail = req.body.useremail; 
  
  if(!User.addUser(name, mail, db)) {
    res.send("There was a problem adding the information to the database.");
    return;
  }
  
  res.redirect('userlist');
});

module.exports = router;

I haven't tested the code because I really don't have time but I hope it works fine.

Dimitar K. Nikolov
  • 130
  • 1
  • 2
  • 10