将参数传递给节点.js中的路由

Passing parameters to routes in node.js

本文关键字:路由 js 节点 参数传递      更新时间:2023-09-26

我对Web开发很陌生。我曾经使用 WPF 和 C# 进行桌面开发。现在我正在学习 Node.js

我有一个名为 Party 的模型.js其中我定义两个导出,如下所示:

module.exports.getAllParties = function(callback){
  Party.find().lean().exec(function(err, parties){
    if (err) return callback(err, null);
    callback(null, parties);
  });
};
module.exports.getPartyByPartyCode = function(partyCode, callback){
  Party.find({partyCode: partyCode}).exec(function(err, party){
    if(err) return callback(err, null);
    callback(null, party);
  });
};

现在,我还有一个名为 Party 的路由.js其中我有两个 get 方法如下:

router.get('/', function(req, res, next){
  //retrieve all parties from Party model
  Party.getAllParties(function(err, parties) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({
              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "parties" : parties
                    });
              },
              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(parties);
              }
          });
        }
  });
});
router.get('/:partyCode', function(req, res, next){
  Party.getPartyByPartyCode(function(err, party) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({
              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "party" : party
                    });
              },
              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(party);
              }
          });
        }
  });
});

现在,当我使用 ajax 时:

var inputElem = $('#partyForm :input[name="partyCode"]'),
    inputVal = inputElem.val(),
    data = { partyCode : inputVal },
    eReport = ''; //error report
$.ajax(
{
    type: "GET",
    url: "/Party",
    dataType: "json",
    data: data,
    beforeSend: function(jqXHR, settings)
    {
        console.log(settings.url);
    },
    success: function(party)
    {
        if (party)
        {
          console.log(party);
          return 'Party ' + party.partyName + ' has already taken party code: ' + party.partyCode + '. Please choose a different PartyCode.';
        }
        else
        {
          console.log("party does not exist.");
           return true;
        }
    },
    error: function(xhr, textStatus, errorThrown)
    {
        alert('ajax loading error... ... '+url + query);
        return false;
    }
});

我的问题是:为什么上面的 ajax 调用返回我所有各方?我只想得到一个其patyCode被传递给ajax调用数据的一方......

路由器响应代码和 ajax 函数中都存在一些错误:

首先更正您的路由器代码:

您未在模型中使用提供的参与方代码。

router.get('/:partyCode', function (req, res, next) {
var partyCode = req.param('partyCode');
Party.getPartyByPartyCode(partyCode, function (err, party) {
    if (err) {
        return console.error(err);
    } else {
        //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
        res.format({
            //response in dust or jade files
            html: function () {
                res.render('Party', {
                    title: 'Party',
                    "party": party
                });
            },
            //JSON response will show all parties in JSON format
            json: function () {
                res.json(party);
            }
        });
    }
});

});

正确的 Ajax 函数调用

您必须提供派对代码作为 URL 参数,因为您的路由器指示类似/:partyCode。请尝试以下操作:

var inputElem = $('#partyForm :input[name="partyCode"]'),
    inputVal = inputElem.val(),
    eReport = ''; //error report
$.ajax({
    type: "GET",
    url: "/"+inputVal,
    dataType: "json",
    data: data,
    beforeSend: function (jqXHR, settings) {
        console.log(settings.url);
    },
    success: function (party) {
        if (party)
        {
            console.log(party);
            return 'Party ' + party.partyName + ' has already taken party code: ' + party.partyCode + '. Please choose a different PartyCode.';
        }
        else
        {
            console.log("party does not exist.");
            return true;
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        alert('ajax loading error... ... ' + url + query);
        return false;
    }
});