mongo is not defined

0

Mam taki błąd proszę o pomoc

C:\Users\Robert\Desktop\FINISH\node_modules\mongodb\lib\utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

ReferenceError: mongo is not defined
    at C:\Users\Robert\Desktop\FINISH\routes\info.js:38:70
    at C:\Users\Robert\Desktop\FINISH\node_modules\async\lib\async.js:122:13
    at _each (C:\Users\Robert\Desktop\FINISH\node_modules\async\lib\async.js:46:13)
    at Object.async.each (C:\Users\Robert\Desktop\FINISH\node_modules\async\lib\async.js:121:9)
    at C:\Users\Robert\Desktop\FINISH\routes\info.js:34:31
    at handleCallback (C:\Users\Robert\Desktop\FINISH\node_modules\mongodb\lib\utils.js:120:56)
    at C:\Users\Robert\Desktop\FINISH\node_modules\mongodb\lib\cursor.js:861:16
    at handleCallback (C:\Users\Robert\Desktop\FINISH\node_modules\mongodb-core\lib\cursor.js:171:5)
    at setCursorDeadAndNotified (C:\Users\Robert\Desktop\FINISH\node_modules\mongodb-core\lib\cursor.js:505:3)
    at nextFunction (C:\Users\Robert\Desktop\FINISH\node_modules\mongodb-core\lib\cursor.js:660:7)

package.json:

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "async": "^0.9.0",
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.10.2",
    "bson": "^4.2.2",
    "client-sessions": "^0.7.0",
    "express": "^4.11.1",
    "express-handlebars": "^2.0.1",
    "mongodb": "^2.2.33"
  }
}


0

Wrzuć zawartość kodu, który napisałeś. W tej chwili można wywnioskować że wywołujesz zmienną, która nie jest zdefiniowana.

0

Proszę:
info.js

var async = require("async");

module.exports = function(app, fns) {

    var dbConnect = fns.helpers.dbConnect,
        handleError = fns.helpers.handleError,
        createObjectId = fns.helpers.createObjectId;

   

        app.get("/info/:colname", function(req, res) {

            var availableNames = ["games", "characters", "categories", "gamerentals", "clients"],
                colname = req.params.colname,
                name = req.query.name,
                regex = new RegExp(name, "ig"),
                fields = [
                    {first_name: regex},
                    {last_name: regex},
                    {title: regex},
                    {name: regex}
                ];
        
            if(availableNames.indexOf(colname) === -1) return handleError(res);
        
            dbConnect(req, res, function(req, res, db) {
        
                if(colname === "gamerentals") {
        
                    db.collection("gamerentals").find({}).toArray(function(err, docs) {
        
                        var count = 0;
        
                        async.each(docs, function(doc, callback) {
        
                            var rent = doc;
        
                            db.collection("games").findOne({_id: new mongo.ObjectID(rent.game_id)}, {fields: {title: 1}}, function(err, doc) {
        
                                if(err) {
                                    callback(err);
        
                                    return;
                                }
        
                                if(doc.title.match(regex)) {
                                    count++;
                                }
        
                                callback();
        
                                return;
        
                            });
        
                        },
                        function(err) {
        
                            if(err) return handleError(res);
        
                            res.status(200);
                            res.set("Content-Type", "text/plain");
                            res.send(String(count));
        
                            db.close();
        
                        });
        
                    });
        
                } else {
        
                    db.collection(colname).count({$or: fields}, function(err, count) {
        
                        if(err) return handleError(res);
        
                        res.status(200);
                        res.set("Content-Type", "text/plain");
                        res.send(String(count));
        
                        db.close();
        
                    });
        
                }
        
            });
        
        });

};

function.js

var mongo = require("mongodb"),
    MongoClient = mongo.MongoClient,
    dbUrl = "mongodb://localhost:27017/wyp";

module.exports = {

    handleError: function (res) {

        res.status(500);
        res.json({
            error: true
        });

    },

    isValidId: function (id) {

        // return mongo.BSONPure.ObjectID.isValid(id);
        return require('mongodb').ObjectID.isValid(id);

    },

    createObjectId: function (id) {

        return new mongo.ObjectID(id);

    },

    dbConnect: function (req, res, callback) {

        MongoClient.connect(dbUrl, function (err, db) {

            if (err) return this.handleError(res);

            callback(req, res, db);

        });

    },

    checkLogin: function (req, res, next) {

        if (req.url === "/login" || req.url === "/register") {
            return next();
        }

        if (req.session && req.session.user) {
            return next();
        } else {

            if (!req.xhr) {
                res.redirect("/login");
            } else {
                res.status(401);
                res.json({
                    error: "logout"
                });
            }

        }

    }

};
1

W każdym module, w którym używasz danej biblioteki, musisz ją osobno zaimportować. Jeśli w jednym pliku masz zmienną mongo:

var mongo = require("mongodb"),

to taka zmienna będzie widoczna tylko w tym module.

1 użytkowników online, w tym zalogowanych: 0, gości: 1