簡體   English   中英

應用程序上傳到 heroku 后如何連接到數據庫

[英]How to connect to database once app is uploaded to heroku

我最近將我的應用程序推送到 heroku 並且該應用程序使用 mongodb 地圖集。 當我在本地主機上運行它時它運行良好,但是當我將它推送到 heroku 服務器時,它給了我這個錯誤:

2021-02-09T03:54:31.379532+00:00 heroku[web.1]: State changed from starting to up
2021-02-09T03:54:55.357747+00:00 app[web.1]: MongooseError: Operation `todotitles.find()` buffering timed out after 10000ms
2021-02-09T03:54:55.357758+00:00 app[web.1]: at Timeout.<anonymous> (/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20)
2021-02-09T03:54:55.357758+00:00 app[web.1]: at listOnTimeout (internal/timers.js:554:17)
2021-02-09T03:54:55.357759+00:00 app[web.1]: at processTimers (internal/timers.js:497:7)
2021-02-09T03:54:55.371677+00:00 heroku[router]: at=info method=GET path="/" host=todolist-app101.herokuapp.com request_id=ba07cea3-44b2-4346-8cda-40b4c81fa1c3 fwd="110.54.169.178" dyno=web.1 connect=0ms service=10043ms status=500 bytes=404 protocol=https
2021-02-09T03:54:55.374468+00:00 app[web.1]: TypeError: /app/views/index.ejs:11
2021-02-09T03:54:55.374470+00:00 app[web.1]: 9|     <a class="site-title" href="/">ToDo App</a>
2021-02-09T03:54:55.374471+00:00 app[web.1]: 10|     <div class="main-list-container">
2021-02-09T03:54:55.374471+00:00 app[web.1]: >> 11|         <% todos.forEach((todos)=>{ %>
2021-02-09T03:54:55.374472+00:00 app[web.1]: 12|                 <form action="/removeList" class="list-container" method="POST">
2021-02-09T03:54:55.374473+00:00 app[web.1]: 13|                     <a href="/list/<%= todos._id %>" class="list-container-title"><%= todos.title %></a>
2021-02-09T03:54:55.374473+00:00 app[web.1]: 14|                     <button type="submit" name="removeList" value="<%= todos._id %>" class="deletebtn">�️</button>
2021-02-09T03:54:55.374474+00:00 app[web.1]:
2021-02-09T03:54:55.374474+00:00 app[web.1]: Cannot read property 'forEach' of undefined

I have made everything right, although I don't know why mongodb atlas doesnt seem to find my collection if I run heroku but on local host I can see my collections in the mongodb atlas Data. 我不知道這有什么問題。

這是 app.js 代碼:


let mongoServerPassword = process.env.MONGO_SERVER_PASSWORD
mongoose.connect('mongodb+srv://admin-cloyd:'+mongoServerPassword+'@todo-app.0pmsv.mongodb.net/toDoDB?retryWrites=true&w=majority', {useNewUrlParser:true, useUnifiedTopology: true })

app.listen(process.env.PORT || 3000, ()=>{
    console.log("listening on port 3000")
})
mongoose.set('useFindAndModify', false);
const TodoSchema = new mongoose.Schema({
    name:  {
        type: String,
        required: true,
        min: 1,
    },
})
const todo = mongoose.model('todo', TodoSchema)

const TodoTitleSchema = new mongoose.Schema({
    title: {
        type: String,
        min: 1,
        max: 30,
    },
    content: [TodoSchema],
})
const todoTitle = mongoose.model('todoTitle', TodoTitleSchema)

app.get('/', (req,res)=>{
    todoTitle.find((err, todos)=>{
        if(err){
            console.log(err)
        }
        res.render('index.ejs', {
            todos: todos,
        })
    })
})
app.post('/addNewList', (req,res)=>{
    let newList = todoTitle.insertMany({
        title: req.body.newList,
    })
    res.redirect('/')
})
app.get('/list/:todoID', (req,res)=>{
    let reqID = req.params.todoID;
    todoTitle.find({},(err, todos)=>{
        todoTitle.findOne({_id:reqID},(err, foundtodo)=>{
            if(err){
                console.log(err)
            }
            res.render('list.ejs', {
                todos: todos,
                todostitle: foundtodo.title,
                todocontent: foundtodo.content,
                todoID: foundtodo._id,
                foundtodo: foundtodo,
            })
        })   
    })
})

app.post('/addNewTodo', (req,res)=>{
    let newItem = new todo({name:req.body.newItem})
    let listtitle = req.body.listTitle;
    todoTitle.findOne({_id:listtitle},(err, foundtodo)=>{
        if(err){
            console.log(err)
        }
        foundtodo.content.push(newItem);
        foundtodo.save();
        res.redirect('/list/'+listtitle);
    })
})

app.post('/removeList',(req,res)=>{
    let listID = req.body.removeList;
    todoTitle.findOneAndDelete({_id:listID}, (err, foundlist)=>{
        if(err){console.log(err)};
        res.redirect('/');
    })
})
app.post('/removeTodo',(req,res)=>{
    let todoID = req.body.removeTodo;
    let listID = req.body.listID;
    todoTitle.findOneAndUpdate({_id: listID}, {$pull: {content: {_id: todoID}}}, (err, foundtodo)=>{
        if(err){
            console.log(err)
        }
        res.redirect('/list/'+listID);
    })
})  

編輯:應用程序上傳到 heroku 時無法連接到數據庫。

我想通了,問題是 heroku 沒有在 mongoose.connect 中讀取這部分:

'mongodb+srv://admin-cloyd:'+mongoServerPassword+'@todo-app.0pmsv.mongodb.net/toDoDB?retryWrites=true&w=majority'

我做了更多的研究,在 heroku 設置中,有一部分說配置變量,我所做的是創建一個名為 MONGODB_URL 的變量,其值為

'mongodb+srv://admin-cloyd:<actual password>@todo-app.0pmsv.mongodb.net/toDoDB?retryWrites=true&w=majority'

然后我編輯了我的 mongoose.connect 到這個:

mongoose.connect(process.env.MONGODB_URL||'mongodb+srv://admin-cloyd:'+mongoServerPassword+'@todo-app.0pmsv.mongodb.net/toDoDB?retryWrites=true&w=majority', {useNewUrlParser:true, useUnifiedTopology: true })

它奏效了。 我不知道為什么,但它有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM