我通过 mongoose 连接到 mongoDB,在 localhost 上使用我的应用程序时没有错误,但是当我将它部署到 Heroku 上时,出现内部服务器错误。
这是我的js代码:
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(session({
secret: varkey,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect("mongodb+srv://admin-name:password@cluster0-7monv.mongodb.net/userDB?retryWrites=true&w=majority", {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.set("useCreateIndex", true);
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String
});
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "https://herokudomain.herokuapp.com/auth/google/callback",
},
function(accessToken, refreshToken, profile, cb) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
app.get("/auth/google", passport.authenticate("google", {scope : ["profile", "email"]}));
app.get("/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/login" }),
function(req, res) {
res.redirect("/");
});
这些是我的html代码:
<a class="btn" href="/auth/google" role="button">Sign Up with Google</a>
Heroku 日志在他们的错误日志中显示了这一点:
2020-06-09T04:05:10.523711+00:00 app[web.1]: MongoError: E11000 duplicate key error collection: userDB.users index: username_1 dup key: { username: null }
2020-06-09T04:05:10.523722+00:00 app[web.1]: at Function.create (/app/node_modules/mongodb/lib/core/error.js:51:12)
2020-06-09T04:05:10.523722+00:00 app[web.1]: at toError (/app/node_modules/mongodb/lib/utils.js:149:22)
2020-06-09T04:05:10.523723+00:00 app[web.1]: at /app/node_modules/mongodb/lib/operations/common_functions.js:265:39
2020-06-09T04:05:10.523723+00:00 app[web.1]: at handler (/app/node_modules/mongodb/lib/core/sdam/topology.js:913:24)
2020-06-09T04:05:10.523724+00:00 app[web.1]: at /app/node_modules/mongodb/lib/cmap/connection_pool.js:356:13
2020-06-09T04:05:10.523724+00:00 app[web.1]: at handleOperationResult (/app/node_modules/mongodb/lib/core/sdam/server.js:489:5)
2020-06-09T04:05:10.523725+00:00 app[web.1]: at MessageStream.messageHandler (/app/node_modules/mongodb/lib/cmap/connection.js:270:5)
2020-06-09T04:05:10.523726+00:00 app[web.1]: at MessageStream.emit (events.js:315:20)
2020-06-09T04:05:10.523726+00:00 app[web.1]: at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
2020-06-09T04:05:10.523726+00:00 app[web.1]: at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
2020-06-09T04:05:10.523727+00:00 app[web.1]: at writeOrBuffer (_stream_writable.js:352:12)
2020-06-09T04:05:10.523727+00:00 app[web.1]: at MessageStream.Writable.write (_stream_writable.js:303:10)
2020-06-09T04:05:10.523728+00:00 app[web.1]: at TLSSocket.ondata (_stream_readable.js:712:22)
2020-06-09T04:05:10.523728+00:00 app[web.1]: at TLSSocket.emit (events.js:315:20)
2020-06-09T04:05:10.523729+00:00 app[web.1]: at addChunk (_stream_readable.js:302:12)
2020-06-09T04:05:10.523729+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:278:9)
当 Web 应用程序返回“内部服务器错误”页面时。似乎他们在说我的数据库有重复,但我已经清除了我的数据库并且那里没有任何内容了。
请帮助我,我对编程很陌生,所以我仍在学习如何阅读错误代码并理解它们,以便我自己调试。
我已经看到了类似的问题并尝试了提供的解决方案,但它们都不适用于我的情况,这就是我决定提出这个问题的原因。