【问题标题】:Express function failing to get data from Mongo Atlas databaseExpress 函数无法从 Mongo Atlas 数据库中获取数据
【发布时间】:2021-01-07 16:32:44
【问题描述】:

我编写了一个快速函数来对 mongo atlas 数据库集合执行 GET 请求。但是,在 Postman 中对其进行测试后,我记录了以下错误: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster.

我已经配置了我的数据库 IP 白名单,以便它可以接受来自任何 IP 的请求,如下所示: 网络访问 -> 编辑 -> 访问列表条目:0.0.0.0/0。

关于它为什么不起作用的任何建议?

这是快速路线:

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

这是模型

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

这里是返回的数据示例:

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

【问题讨论】:

标签: mongodb express mongoose


【解决方案1】:

我设法解决了这个问题 - 我在架构中输入了错误的集合名称。我将它从“review”更改为“reviews”,这使得 GET 请求有效。

【讨论】:

    猜你喜欢
    • 2019-12-22
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2020-04-22
    • 2017-04-23
    • 2018-05-28
    相关资源
    最近更新 更多