【问题标题】:Node js express - My query isnt returning what i exspect, any thoughts?Node js express - 我的查询没有返回我所期望的,有什么想法吗?
【发布时间】:2016-12-09 09:37:28
【问题描述】:

当我使用http://localhost:3000/buyersearch 运行这条路线时,我得到“请输入买家识别号”。但是当我跑步时 http://localhost:3000/buyersearch?buyerID=2447 它应该使用我制作的 ejs 文件输出。但相反,我得到“没有找到具有该 ID 号的订单”。 2447!它存在于我的数据库中。怎么还不上来?

谢谢

       **Here's my JSON**

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

这是我正在做的查询的 buySearch.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>

【问题讨论】:

  • java != javascript
  • 您的请求显示买家 ID 为 2447,但示例 JSON 显示买家 ID 为 2299 - 数据库中 2447 的数据是什么?
  • 那是我数据库中的另一个文档,无论如何它应该提取的越少:(

标签: javascript node.js express ejs


【解决方案1】:

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

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

【讨论】:

  • 它仍然返回 No order found with that ID number with 2447
  • 为什么会进入那个区块?其中哪一个是trueerr || !result || result.length == 0
  • prntscr.com/dh4j8f 现在显示这个,我想知道为什么它不显示我通过 ejs 找到的买家 ID。我的 ejs 文件正确吗?
【解决方案2】:
A few minor tweaks and help from Dave resolved my issue :D 

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

    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 || !parseInt(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": parseInt(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('buyerSearch', {
                                buyerSearch: result[0],
                                buyerID: result[0]
                            })
                        }

                    });

                }

            })
        }

    });

    module.exports = router;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2021-12-11
    • 2018-05-11
    相关资源
    最近更新 更多