如何通过单独的模块将值发送到数据库?

时间:2015-07-13 12:39:12

标签: javascript node.js mongodb express monk

我创建了一个基本的快递项目。我在根文件夹中创建了一个名为lib / userhandler.js的文件。



//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");
    }
  });
}




在我的routs / users.js文件中,每当加载用户页面时,我想通过userhandler.js将名称和邮件值发送到数据库。



//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;




当我尝试加载用户页面时,它会显示"发送后无法设置标题。"

谢谢

2 个答案:

答案 0 :(得分:0)

您应该尝试通过类似这样的回调将错误从数据库返回到路由处理程序:

//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);
  });
}

答案 1 :(得分:0)

不应该插入请求响应对象作为 addUser()。它们应该在路由器回调函数中。我在函数中添加了一个新参数,因此您可以将数据库作为参数传递,这要归功于接收请求对象作为参数的路由器。



//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
}




我也在这里更改了代码,以便 req res < / em>参数。

&#13;
&#13;
//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;
&#13;
&#13;
&#13;

我还没有对代码进行测试,因为我真的没有时间,但我希望它能正常运行。