Pracuję nad interfejsem API logowania.

Backend: Node.JS Datastore: MongoDB

Dostaję 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;

Schemat: 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);

Połączenie MongoDB: 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
};

Oto błąd, który otrzymuję:

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  

Pomoc byłaby bardzo mile widziana

1
Shreyas Heda 2 kwiecień 2020, 16:15

3 odpowiedzi

Najlepsza odpowiedź

Użyj modelu Mongoose bezpośrednio, aby uzyskać dokumenty

Zamiast

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

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

Spróbuj użyć swojego modelu Mongoose bezpośrednio, jak:

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

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

});
2
Pascal Lamers 4 kwiecień 2020, 07:23

Db.collection () to metoda dostarczana z powłoką mongo.

Możesz skorzystać z biblioteki mongoose, aby rozwiązać problem.

0
jcoleau 6 kwiecień 2020, 18:46

Używasz złego interfejsu API. .find(..) jest dla Mongoose i używasz go do konfiguracji bazy danych db.collection(..)

Zobacz zmodyfikowane 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;
0
Ayman Arif 2 kwiecień 2020, 13:37