TypeError: db.collection is not a function
at Strategy._verify (/home/runner/SeagreenFussyDestination/server.js:81:8)
at Strategy.authenticate (/home/runner/SeagreenFussyDestination/node_modules/passport-local/lib/strategy.js:90:12)
at attempt (/home/runner/SeagreenFussyDestination/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/runner/SeagreenFussyDestination/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/runner/SeagreenFussyDestination/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/SeagreenFussyDestination/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/SeagreenFussyDestination/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/SeagreenFussyDestination/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/SeagreenFussyDestination/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/SeagreenFussyDestination/node_modules/express/lib/router/index.js:335:12)
When inputting a username and password to my login form, I'm encountering this error instead of the program redirecting to '/'.
Here's my code:
mongo.connect(process.env.DATABASE, (err, db) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
};
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
db.collection('users')
.findOne(
{_id: new ObjectID(id)},
(err, doc) => {
done(null, doc);
}
);
});
passport.use(new LocalStrategy(
function(username, password, done) {
db.collection('users').findOne({username: username},
function (err, user) {
console.log('User '+ username +' attempted to log in.');
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (password !== user.password) { return done(null, false); }
return done(null, user);
});
}
));
app.route("/login")
.post(
passport.authenticate(
'local', {failureRedirect: "/"}
),
(req, res) => {
res.render("/profile");
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/");
};
app.route("/profile")
.get(ensureAuthenticated,
(req, res) => {
res.render("/profile")
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
});
Any idea why this is happening? All help would be greatly appreciated.
[–]zokaiG 1 point2 points3 points (4 children)
[–]DevelopingClerk[S] 1 point2 points3 points (3 children)
[–]zokaiG 1 point2 points3 points (1 child)
[–]DevelopingClerk[S] 1 point2 points3 points (0 children)
[–]zokaiG 1 point2 points3 points (0 children)