Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Bizarre Node.js error

DennisM

New Coder
I'm trying to do some basic stuff with MongoJS. When I try to run this simple program:

JavaScript:
var database = "testingMongoJS";
var collection = "testCollection";

var db = mongojs(database, collection);

db.on("error", function(error){
    console.log("Database: ", error);

})

app.get("/", function(req, res){
    db.testCollection.find({}, function(error, found){
        console.log(found);

    })
})

I get this error:

Code:
C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\lib\database.js:57
  cols.forEach(function (colName) {
       ^

TypeError: cols.forEach is not a function
    at new Database (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\lib\database.js:57:8)
    at module.exports (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\index.js:5:12)
    at Object.<anonymous> (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\myserver.js:12:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

What's the deal?
 
I think that collection with mongojs needs to be a collection, not a string or something.
You could also try to implement forEach function on collection but I think it needs to be a collection such as an array.
Try with database and/or colllection as an array or dictionary.
X E.
 
I'm trying to do some basic stuff with MongoJS. When I try to run this simple program:

JavaScript:
var database = "testingMongoJS";
var collection = "testCollection";

var db = mongojs(database, collection);

db.on("error", function(error){
    console.log("Database: ", error);

})

app.get("/", function(req, res){
    db.testCollection.find({}, function(error, found){
        console.log(found);

    })
})

I get this error:

Code:
C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\lib\database.js:57
  cols.forEach(function (colName) {
       ^

TypeError: cols.forEach is not a function
    at new Database (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\lib\database.js:57:8)
    at module.exports (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\node_modules\mongojs\index.js:5:12)
    at Object.<anonymous> (C:\Users\Dennis\Desktop\UTAUS201801FSF2-FT-Class-Repository-FSF-FT\class-content\18-mongo-mongoose\01-Activities\11-Scraping-into-a-db\Solved\myserver.js:12:10)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

What's the deal?
Best work around, although not as optimal: use vanilla for loop
 
Back
Top Bottom