-1

我创建了一个基本的快递项目。我在根文件夹中创建了一个名为 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;

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

谢谢你

4

2 回答 2

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);
  });
}
于 2015-07-13T12:55:11.243 回答
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
}

我也更改了这里的代码,以便可以从reqres参数接收nameemail变量。

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

我没有测试代码,因为我真的没有时间,但我希望它能正常工作。

于 2015-07-13T12:55:35.900 回答