我是Node和Mongo的新手。尝试使用mongoDB Atlas开发Web应用程序及其数据库。并卡在上面的错误。我正在使用以下文件。
index.js
const express = require("express");
const mongoose = require("mongoose");
const keys = require("./config/keys");
require("./models/user");
require("./services/passport");
mongoose.connect(keys.mongoURI);
const app = express();
require("./routes/authRoutes")(app);
const PORT = process.env.PORT || 5000;
app.listen(PORT);
user.js
const mongoose = require("mongoose");
const { Schema } = mongoose;
const userSchema = new Schema({
googleID: String,
});
mongoose.model("users", userSchema);
passport.js
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const keys = require("../config/keys");
const mongoose = require("mongoose");
const User = mongoose.model("users");
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: "/auth/google/callback",
},
(accessToken, refreshToken, profile, done) => {
new User({
googleID: profile.ID,
}).save();
}
)
);
authRouter.js
const passport = require("passport");
module.exports = (app) => {
app.get(
"/auth/google",
passport.authenticate("google", {
scope: ["profile", "email"],
);
app.get("/auth/google/callback", passport.authenticate("google"));
};
keys.js
module.exports = {
googleClientID:
“ xxxxxxxxxxxx-3lxxxxll0tflxxxxxxxxov05ovohrvv8.apps.googleusercontent.com”, googleClientSecret:“ - ” , mongoURI: “ mongodb + srv:// userMe: *****************@emaily.fepbh.mongodb.net/testdb?retryWrites=true&w=majority”, };
我在终端中收到此错误。
=> (node:25066) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [emaily-shard-00-00.fepbh.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to emaily-shard-00-00.fepbh.mongodb.net:27017 closed]
未创建数据库或集合作为结果。请帮我在这里。非常感谢。