【问题标题】:Node JS - Express - How do i search the mongoDB object_id within the local host browser?Node JS - Express - 如何在本地主机浏览器中搜索 mongoDB object_id?
【发布时间】:2016-12-09 23:15:19
【问题描述】:

如何在浏览器中搜索 object_id?有任何想法吗?我试过 localhost:3000/objectsearch?objectid=58499f5ed0055acc431a99f0 但这只会显示“请输入 objectID 编号并且不会重定向我”以查看。 如果有人看到解决方案,请告诉我,谢谢

   **objectSearch.js**

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



router.get('/', function (req, res) { //http://localhost:3000/objectSearch
    var _id = req.query.objectId;

    if (!_id || !parseInt(_id)) {
        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({_id: mongodb.ObjectID(_id)}).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('objectSearch', {
                            objectSearch: result[0],
                            title: result[0]
                        })
                    }

                });

            }

        })
    }

});

module.exports = router;



**objectSeach.ejs**

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

【问题讨论】:

  • 您在objectid 中有错字,您在浏览器中传递了objectid=58499f5ed0055acc431a99f0,但在您的路由器中需要req.query.objectId
  • 我不明白
  • 您的objectid 查询字符串的i 字母是小写的,但您的路由器需要objectId,您可以将路由器中的req.query.objectId 更改为req.query.objectid 或更改在浏览器中查询objectid字符串到objectId
  • alpha 发布答案,以便我可以将其作为答案:D

标签: javascript node.js express ejs


【解决方案1】:

你的objectid查询字符串的i字母是小写的,但你的路由器期待objectId,你可以将路由器中的req.query.objectId更改为req.query.objectid或更改@987654326的查询字符串在浏览器中@objectId

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2019-05-11
    • 2018-05-21
    • 2023-03-14
    • 2015-10-22
    相关资源
    最近更新 更多