【发布时间】:2022-07-30 04:18:21
【问题描述】:
您好,我正在按照 youtube 教程 tutorial link 进行预订系统
我已经把 app.js 文件变成了这样(如下所示):
const express = require('express');
const bodyParser = require('body-parser');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const mongoose = require('mongoose');
const User = require('./models/user')
const Event = require('./models/events');
const app = express();
app.use(bodyParser.json());
app.use(
'/graphql',
graphqlHTTP({
schema: buildSchema(`
type Event {
_id: ID!
title: String!
description: String!
price: Float!
date: String!
}
type User {
_id: ID!
username: String!
studentnumber: String
}
input EventInput {
title: String!
description: String!
price: Float!
date: String!
}
type UserInput {
username: String!
studentnumber: String!
}
type RootQuery {
events: [Event!]!
}
type RootMutation {
createEvent(eventInput: EventInput): Event
createUser(userInput: UserInput): User
}
schema {
query: RootQuery
mutation: RootMutation
}
`),
rootValue: {
events: () => {
return Event.find()
.then(events => {
return events.map(event => {
return { ...event._doc, _id: event.id };
});
})
.catch(err => {
throw err;
});
},
createEvent: args => {
const event = new Event({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: new Date(args.eventInput.date)
});
return event
.save()
.then(result => {
console.log(result);
return { ...result._doc, _id: result._doc._id.toString() };
})
.catch(err => {
console.log(err);
throw err;
});
},
},
graphiql: true
})
);
mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.scgam.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`
)
.then(() => {
app.listen(3002);
})
.catch(err => {
console.log(err);
});
但这给出了这个错误信息:
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the data
base from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/securi
ty-whitelist/
at NativeConnection.Connection.openUri (C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\connection.js:8
07:32)
at C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\index.js:342:10
at C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:1
0)
at Mongoose._promiseOrCallback (C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\index.js:1181:10)
at Mongoose.connect (C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\node_modules\mongoose\lib\index.js:341:20)
at Object.<anonymous> (C:\Users\Abdulrahman\Documents\GitHub\2021-StudySkillsApp\Bookings\app.js:89:6)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'cluster0-shard-00-00.scgam.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.scgam.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.scgam.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-jlm5p9-shard-0',
logicalSessionTimeoutMinutes: undefined
},
code: undefined
}
[nodemon] clean exit - waiting for changes before restart
而且我确信我将我的家庭 IP 作为我可以连接的 IP,并且使用户名+密码+dbname 正确(它们位于我通过 process.env.variablename 调用的 nodemon.json 文件中)和链接的 URL 有效
如果有帮助,这也是我在搜索 localhost:3002/graphql 时得到的 任何帮助都会很好,因为我需要它才能工作谢谢:)
【问题讨论】:
-
Title 应该已经在您的 MongoDB Atlas 集群中,但不允许我这样做,所以我把它放在这里以防万一
标签: mongodb graphql node-modules express-graphql