节点js表达 - 我的查询没有返回我所期待的,任何想法?

时间:2016-12-09 00:21:11

标签: javascript node.js express ejs

当我使用http://localhost:3000/buyersearch运行此路线时,我得到"请输入买方识别号码"如预期的那样。但是当我跑步的时候 http://localhost:3000/buyersearch?buyerID=2447它应该使用我制作的ejs文件输出。但相反,我得到了#34;没有找到带有该ID号的订单"。 2447!它存在于我的数据库中。为什么它没有恢复?

由于

       **Here's my JSON**

       {"buyerID" : 2447,
        "name" : {
                "f" : "Jai",
                "s" : "Kishner"
        },
        "gender" : "Male",
        "birthDate" : "1996-08-13",
        "student" : true,
        "appOS" : "Android"}

继承我的buyerSearch.js以查询我正在做的

var express = require('express');
var router = express.Router();
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/WishList';


//http://localhost:3000/buyersearch?buyerID=2447


//buyerIDSearch
router.get('/', function (req, res) {
    var buyerID = req.query.buyerID;

    if (!buyerID) {
        res.render('error', {message: "Please enter an Buyer Identification Number", error: {status: "", stack: ""}});
    } else {
        mongoClient.connect(url, function (err, db) {
            if (err) {
                res.render('error', {message: "Failed to connect", error: {status: "", stack: ""}});
            } else {
                var WishListDB = db.collection('orders');
                WishListDB.find({"_buyerID": (buyerID)}).toArray(function (err, result) {
                    if (err || !result || result.length == 0) {
                        res.render('error', {
                            message: "No order found with that ID number",
                            error: {status: "", stack: ""}
                        });
                    } else {
                        res.render('order', {
                            order: result[0]
                        })
                    }

                });

            }

        })
    }

});

module.exports = router;


**Heres my buyerSearch.ejs file that I'm using to output the buyer ID once entered**

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1></h1>
<p>Welcome</p>

<p><%buyerSearch.buyerID %></p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

对您的收藏集的查询应使用"buyerID"而不是"_buyerID",如下所示:

WishListDB.find({"buyerID": buyerID}).toArray()

答案 1 :(得分:0)

[
  {
    "key": "K1204", 
    "values": [
      {
        "key": "Both", 
        "values": [
          {
            "key": 1420070400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1422748800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1425168000000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1427846400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1430438400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1433116800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1435708800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1438387200000, 
            "values": 27927.734942179697
          }, 
          {
            "key": 1441065600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1443657600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1446336000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1448928000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1451606400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1454284800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1456790400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1459468800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1462060800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1464739200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1467331200000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1470009600000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1472688000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1475280000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1477958400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1480550400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1483228800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1485907200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1488326400000, 
            "values": 36929.200142179696
          }
        ]
      }
    ]
  }
]