首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将帖子路由到阿特拉斯mongoDB

无法将帖子路由到阿特拉斯mongoDB
EN

Stack Overflow用户
提问于 2021-01-28 03:02:54
回答 2查看 25关注 0票数 0

我已经创建了用户模型,注册和认证登录路由。Atlas Mongodb创建了新集合。我可以将注册发布到db并登录,所以它工作得很好。

后来我添加了绳索模型和crud路由,我可以访问get路由,但当我尝试将新的绳索发布到数据库时,错误被触发,新的集合没有添加到atlas MongoDb中。我不明白为什么。

绳索模型Rope.js:

代码语言:javascript
复制
const mongoose = require('mongoose')

const ropeSchema = new mongoose.Schema({
    name:{
        type: String,
        required: true,

    },

    description:{
        type: String,
        required: true,

    }
   
})

module.exports = mongoose.model('Rope', ropeSchema)

路由文件ropes.js:

代码语言:javascript
复制
const router = require('express').Router();
const Rope = require('../model/Rope')

router.post('/', async (req, res) => {

    //create a new user
    const rope = new Rope({
        name: req.body.name,
        description: req.body.description
    });
    try {
        await rope.save();
        res.json(rope);
    } catch (err) {
        res.status(500 ).send(err);
    }
});

module.exports = router;

index.js文件:

代码语言:javascript
复制
const mongoose = require('mongoose')
const authRoute = require('./routes/auth');
const ropesRoute = require('./routes/ropes')
const bodyparser = require('body-parser')

app.use(bodyparser.json());


dotenv.config()

mongoose.connect(process.env.DB_CONNECT, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, () => console.log('connected to db'))

app.use(express.json())


app.use('/api/user', authRoute);
app.use('/api/ropes', ropesRoute)


app.listen(3000, () => console.log('up and running'))
EN

回答 2

Stack Overflow用户

发布于 2021-01-28 03:26:38

您可能需要发布错误消息以确定问题所在。

尝尝这个。

代码语言:javascript
复制
   //....
   try{
      const newRope = await rope.save();
       res.status(201).json(newRope);
  } catch (err){
    res.status(500).send(err);
  }
票数 0
EN

Stack Overflow用户

发布于 2021-01-28 06:06:41

问题是,阿特拉斯Mongodb不允许我的IP发布,但我已经将其列入白名单。

所以我把所有地方都列入了白名单,现在我可以发帖了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65925697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档