1

I am working on a Login api.

Backend: Node.JS Datastore: MongoDB

I am getting TypeError

app.js:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.json());

var db = require('./db')
db.connect(() => {
  app.listen(3000, function (){
      console.log(`Listening`);
  });
});


const loginRoute = require('./routes/login');
app.use('/login', loginRoute);

Router - login.js

const express = require('express');
const router = express.Router();
const loginSchema = require('../models/LoginSchema');
const db = require('../db');

router.get('/', (req, res) => {
    db.collection('login').find({}, function (err, result) {
        console.log(result);
        res.send('We are at login api'+result);
    });
});

router.post('/', (req, res) => {
    const logindata = new loginSchema({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
    });
    db.collection('login').insert(logindata, function(err, data) {
        res.json(data);
    });
});

module.exports = router;

Schema : loginschema.js

const mongoose = require('mongoose');

const loginschema = mongoose.Schema({
    name: {
        type : String
    },
    email: {
        type : String,
        required : true
    },
    password: {
        type: String,
        require: true
    }
});

module.exports = mongoose.model('login', loginschema);

MongoDB connection : db.js

const mongoClient = require('mongodb').MongoClient;
const mongoDbUrl = 'mongodb://127.0.0.1:27017';
let db;

function connect(callback){
    mongoClient.connect(mongoDbUrl, { useUnifiedTopology: true }, (err, client) => {
        db = client.db('test');
        callback();
    });
}
function get(){
    return db;
}

function close(){
  db.close();
}

module.exports = {
    connect,
    get,
    close
};

This is the error that I am getting:

TypeError: db.collection is not a function  
    at C:\rest-api\routes\login.js:19:8  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at next (C:\rest-api\node_modules\express\lib\router\route.js:137:13)  
    at Route.dispatch (C:\rest-api\node_modules\express\lib\router\route.js:112:3)  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at C:\rest-api\node_modules\express\lib\router\index.js:281:22  
    at Function.process_params (C:\rest-api\node_modules\express\lib\router\index.js:335:12)  
    at next (C:\rest-api\node_modules\express\lib\router\index.js:275:10)  
    at Function.handle (C:\rest-api\node_modules\express\lib\router\index.js:174:3)  
    at router (C:\rest-api\node_modules\express\lib\router\index.js:47:12)  
    at Layer.handle [as handle_request] (C:\rest-api\node_modules\express\lib\router\layer.js:95:5)  
    at trim_prefix (C:\rest-api\node_modules\express\lib\router\index.js:317:13)  
    at C:\rest-api\node_modules\express\lib\router\index.js:284:7  
    at Function.process_params (C:\rest-api\node_modules\express\lib\router\index.js:335:12)  
    at next (C:\rest-api\node_modules\express\lib\router\index.js:275:10)  
    at C:\rest-api\node_modules\body-parser\lib\read.js:130:5  

Help would be greatly appreciated

3
  • You are calling db.collection but collection is not exported in db.js Commented Apr 2, 2020 at 13:19
  • @ShreyasHeda Where do you get that "collection() " would be a member of the mongodb connection ? It clearly isn't. Remember you are using "mongoose" , not the native mongodb driver. This and what Evert said Commented Apr 2, 2020 at 13:20
  • @ShreyasHeda Please take a look at my answer below, let me know if it helps ! Commented Apr 3, 2020 at 8:17

3 Answers 3

2

Use Mongoose Model directly to get Documents

Instead of

db.collection('login').find({}, function (err, result) {

});
// This will fail since 'collection' is not exported by your db.js

try using your Mongoose Model directly like :

// find all Login Documents 
loginSchema.find({}, function (err, result) {

});
// create a new document
const newlogin = new loginSchema(); 
newlogin.save((err, saveddoc) => {

});
Sign up to request clarification or add additional context in comments.

7 Comments

i tried your solution.. but i am not getting any response. in postman it says : Could not get any response
If result is empty ... this could mean there are no documents in the logins collection
i have two records inside login collection
Curious, did you use the mongoose model to create the documents as in new loginSchema() ? ... by default if the model is called "Login" the collection would be called "Logins" (plural) ... and would be automatically created.
no.. i did not use loginSchema to create document!! i created manually!!
|
0

You are using the wring API. .find(..) is for Mongoose and you are using it for database configuration db.collection(..)

See modified app.js:

const express = require('express');
const router = express.Router();
const loginSchema = require('../models/login');
const db = require('../db');

router.get('/', (req, res) => {
    loginSchema.find({}, function (err, result) {
    if (err) {
      console.log("Error:", err);
    }
    else{
     res.send('We are at login api'+result);
    }   
    });
});

router.post('/', (req, res) => {
    const logindata = new loginSchema({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
    });
    loginSchema.insert(logindata, function(err, data) {
        res.json(data);
    });
});

module.exports = router;

Comments

0

db.collection() is a method provided with the mongo shell.

You can use the mongoose library to solve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.