req.params在猫鼬中不起作用findOne

时间:2018-10-12 06:50:53

标签: javascript node.js express mongoose

我有传递参数的函数

fetchprofile() {

    let uri = "http://localhost:4000/data/username/" + this.tintin;
    this.axios.post(uri).then(response => {

    this.fname = response.data.fname;
    this.lname = response.data.lname;

     });

    }

这是路线:

adnmastroutes.route('/username/:usertin').post(function (req, res) {
console.log('TIN No. of user is:' + req.params.usertin);

adnmastmainmodel.findOne({tin: req.params.usertin}, function (err, user) {

if (err) {
  console.log(err);
}
else {

  console.log(user);
  res.json(user);
} }); });

现在在线

console.log('TIN No. of user is:' + req.params.usertin);

传递的参数req.params.usertin有一个值。 但是为什么要排队

adnmastmainmodel.findOne({tin: req.params.usertin}, function (err, user) {

传递的参数req.params.usertin没有值。

(该行:  adnmastmainmodel.findOne({tin:req.params.usertin},函数(错误,用户){  返回一个空响应。但是当我以这种方式更改它时:  adnmastmainmodel.findOne({tin:'012349876'},函数(错误,用户){  它会返回所需的响应。)

请帮助我如何解决此问题?

p.s。参数this.tintin的值来自sessionStorage.getItem

1 个答案:

答案 0 :(得分:0)

使用toString()函数确保参数为字符串

adnmastmainmodel.findOne({tin: req.params.usertin.toString() }, function (err, user)