Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

mongodb - Express function failing to get data from Mongo Atlas database

I'm written an express function to perform a GET request on a mongo atlas database collection. However, upon testing it in Postman I get the following error logged: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster.

I've configured my database IP whitelist so that it can take requests from any IP, as follows: Network Access -> EDIT -> Access list Entry: 0.0.0.0/0.

Any suggestions as to why it isn't working?

Here's the express route:

app.get('/api/all-reviews', (req,res) => {
    Review.find()
    .then((result) => {
        res.send(result)
    })
    .catch(err => {
        console.log(err)
    })
})

Here's the model

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const reviewSchema = new Schema({
    userName:String,
    stars:String,
    title:String,
    photo:String,
    blurb:String
}, {timestamps:true})

const Review = mongoose.model('review', reviewSchema)

module.exports = Review

And here's a sample of the data being returned:

id:5ff59a7a027e312bb815b3c4
userName:"testuserName"
stars:"1"
title:"title"
photo:"photoURL"
blurb:"blurb"
createdAt:2021-01-06T11:09:46.505+00:00
updatedAt:2021-01-06T11:09:46.505+00:00

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to solve this problem - I had put the wrong collection name in my schema. I changed it from 'review' to 'reviews', which made the GET request work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...