我是NodeJS的初学者。对于登录功能,我需要设置“护照”以处理快速身份验证。

这是我的index.js

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

const path = require ('path');
//const passport = require ('passport');

const {mongoose} = require ('./db.js');
var employeeController = require('./controllers/employeeController.js');
var reservationController = require('./controllers/reservationController.js');

var app = express();
app.use(bodyParser.json());
app.use(cors({origin: 'http://localhost:4200'}));

app.listen(3000, () => console.log('server started at port :3000'));

app.use('/employees',employeeController);
app.use('/reservations',reservationController);


在这里我注释了护照

This is my employee controller which retrieve data

const express = require ('express');
var router= express.Router();
var ObjectId = require('mongoose').Types.ObjectId;

var {Employee} = require('../models/employee');

// =>localhost:3000/employees
router.get('/',(req,res) => {
    Employee.find((err,docs) => {
        if(!err) {res.send(docs);}
        else {console.log('error in retrieving employees :' + JSON.stringify(err,undefined,2)); }

        });
    });

    router.get('/:id',(req,res)=>{
         if(!ObjectId.isValid(req.params.id))
             return res.status(400).send('No record with given id : ${req.params.id}');

            Employee.findById(req.params.id,(err,doc) =>{
                if(!err){res.send(doc);}
                else{console.log('error in retriving employee:'+JSON.stringify(err,undefined,2));}
            });
    });



    router.post('/',(req,res) =>{
        console.log(req.body);
        var emp = new Employee ({
            name: req.body.name,
            position: req.body.position,
            office: req.body.office,
            salary: req.body.salary,
        });
        emp.save((err,doc) => {
            if(!err) {res.send(doc);}
            else{console.log('error in saving employee'+ JSON.stringify(err,undefined,2));}
        });
    });

    router.put('/:id', (req, res)=>{
        if(!ObjectId.isValid(req.params.id))
            return res.status(400).send('No record with given id : ${req.params.id}');
            var emp = {
                name: req.body.name,
                position: req.body.position,
                office: req.body.office,
                salary: req.body.salary,
            };

            Employee.findByIdAndUpdate(req.params.id,{$set:emp},{new:true},(err,doc) =>{
                if(!err){res.send(doc);}
                else{console.log('error in updating employee:'+JSON.stringify(err,undefined,2));}
            });

        });

        router.delete('/:id',(req,res)=>{
            if(!ObjectId.isValid(req.params.id))
                return res.status(400).send('No record with given id : ${req.params.id}');

               Employee.findByIdAndRemove(req.params.id,(err,doc) =>{
                   if(!err){res.send(doc);}
                   else{console.log('error in deleting employee:'+JSON.stringify(err,undefined,2));}
               });
       });

module.exports = router;


我的应用程序正在从mongodb中完美地检索数据。但是,当我在index.js中删除护照中的注释时,数据检索不会发生。添加护照时,我不知道出了什么问题。
因为我是NodeJS的新手,请帮助我。

最佳答案

护照不是中间件。

const path = require ('path');
const passport = require ('passport');
....
app.use(passport.initialize())
.... //your routes


希望这能解决您的问题

关于node.js - 为什么我在NodeJS中导入护照时为什么无法从MongoDB获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51003462/

10-11 07:23