首页 > 解决方案 > 为什么我在heroku上部署它时它不能正常工作?

问题描述

我设计了一个评论系统,它可以在本地主机上完美运行。但是在我将它部署到 Heroku 之后(我使用 mLab 而不是本地 mongoDB),它不能正常工作 --- 提交后新评论不会显示在页面上。

我很困惑,我只是将本地 mongoDB 更改为 mLab mongoDB。除此之外,我没有改变任何其他东西。

下面是我的评论系统的 node.js 代码:

var EnglishofWords = [];
var CommentsArr = []; 
mongoose.connect("mongodb://username:password@ds215172.mlab.com:15172/gredictionary")

app.post("/searchresult/comments", isLoggedIn, function(req, res){
var commentsText = req.body.text;
CommentsArr.push([commentsText, true]);
var EnglishofWord = EnglishofWords[EnglishofWords.length - 1];
var commentText = CommentsArr[CommentsArr.length - 1];

if (CommentsArr.length > 0) {
    if (commentText[1] == true) {
        grewords.findOne({English: EnglishofWord}, function(err, word){
            if (err) {
                console.log(err);
            } else {
                console.log("this step!")
                comments.create({text: commentText[0], author: {id: req.user._id, username: req.user.username}, likes: 0, dislikes: 0}, function(err, comment){
                  if (err) {
                      console.log(err);
                  } else {
                      console.log("comment has been sumbited successfully!");
                      word.Comments.push(comment);
                      word.save();
                  }
                });
            }
        }); 
        CommentsArr[CommentsArr.length - 1][1] = false;
    } 
} 

res.redirect("/searchresult");
})

///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ////////

app.get("/searchresult", function(req, res){
var EnglishofWord = EnglishofWords[EnglishofWords.length - 1];
var WORDS = [];

grewords.findOne({English: EnglishofWord}).populate("Comments").exec(function(err, allgrewords){
    if (err) {
        console.log(err);
    }   else {
        if (allgrewords == null || allgrewords.length < 1 || allgrewords == undefined ) {
            res.render("errorpage");
        } else {
            if (req.isAuthenticated()) {
                User.findOne({username: req.user.username}, function(err, user){
                    if (err) {
                        console.log(err);
                    } else {
                        grewords.findOne({English: EnglishofWord}, function(err, word){
                                if (err) {
                                    console.log(err);
                                } else {
                                    user.words.forEach(function(Word){
                                        WORDS.push(Word.English);
                                    });

                                    if (WORDS.includes(word.English)) {
                                        res.render("searchresult", {greword:allgrewords, signForUser: true});
                                } else {
                                    res.render("searchresult", {greword:allgrewords, signForUser: false});
                                }
                    }
        });
    }
});
            } else {
                res.render("searchresult", {greword:allgrewords, signForUser: "notApplicable"});
            }
        }
    }
});
});

有两个数据库:

var mongoose = require("mongoose");
var comments = require("./comments")

mongoose.connect("mongodb://username:password@ds215172.mlab.com:15172/gredictionary")

var words = [
{ English: "abstemious", Chinese: "(吃喝等)有节制的,节俭的", Synonym: 
"continent", Antonym: "indulgent", Wordlist: false},
];

var wordsSchema = new mongoose.Schema({
English: String,
Chinese: String,
Synonym: String,
Antonym: String,
Wordlist: Boolean,
Comments: [
    {
        type:mongoose.Schema.Types.ObjectId,
        ref:"comments"
    }
]
});

wordsSchema.set('autoIndex', false);

var grewords = mongoose.model("grewords", wordsSchema);

for ( var i = 0; i < words.length; i++) {
grewords.create(words[i], function(err, word){
    if (err) {
        console.log(err);
    } else {
        console.log("DONE");
    }
});
}

///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ////////

var mongoose = require("mongoose");

mongoose.connect("mongodb://username:password@ds215172.mlab.com:15172/gredictionary")

var commentsSchema = new mongoose.Schema({
text: String,
likes: Number,
dislikes: Number,
author: {
    id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
    username: String,
}
});

module.exports = mongoose.model("comments", commentsSchema);

奇怪的是,我在Heroku上运行我的代码,提交评论后,没有“这一步”和“评论已提交成功!” 在控制台中,这意味着代码没有正常运行。但实际上,我提交的新评论已成功添加到 mLab 数据库中的集合中。我不知道这个问题,有人可以帮我解决这个问题吗?非常感谢!

以下是我的heroku日志的一部分:

2018-08-08T12:32:51.592865+00:00 app[web.1]: (node:20) 
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This 
error originated either by throwing inside of an async function without 
a catch block, or by rejecting a promise which was not handled with 
.catch(). (rejection id: 1)
2018-08-08T12:32:51.592964+00:00 app[web.1]: (node:20) [DEP0018] 
DeprecationWarning: Unhandled promise rejections are deprecated. In the 
future, promise rejections that are not handled will terminate the 
Node.js process with a non-zero exit code.
2018-08-08T12:32:51.812271+00:00 heroku[router]: at=info method=GET 
path="/searchresult" host=agile-everglades-19141.herokuapp.com 
request_id=d9275019-8f38-4a87-a171-d9fde7ae0a45 fwd="183.156.167.186" 
dyno=web.1 connect=1ms service=15ms status=304 bytes=152 protocol=https
2018-08-08T12:32:52.103145+00:00 heroku[router]: at=info method=GET 
path="/javascript/searchresult.js" host=agile-everglades- 
19141.herokuapp.com request_id=69d58c11-0588-4b84-8a04-28bb2c8edf1e 
fwd="183.156.167.186" dyno=web.1 connect=1ms service=48ms status=304 
bytes=237 protocol=https
2018-08-08T12:32:52.074730+00:00 heroku[router]: at=info method=GET 
path="/css/searchresult.css" host=agile-everglades-19141.herokuapp.com 
request_id=d4fb1310-d87e-4260-8472-ae0d08663335 fwd="183.156.167.186" 
dyno=web.1 connect=1ms service=21ms status=304 bytes=237 protocol=https
2018-08-08T12:32:52.318480+00:00 heroku[router]: at=info method=GET 
path="/background_of_searchresult.jpg" host=agile-everglades- 
19141.herokuapp.com request_id=96c60d28-765f-4c6d-aac9-78c0cf4a8fa8 
fwd="183.156.167.186" dyno=web.1 connect=1ms service=8ms status=304 
bytes=240 protocol=https
2018-08-08T13:08:52.907516+00:00 heroku[web.1]: Idling
2018-08-08T13:08:52.908137+00:00 heroku[web.1]: State changed from up to 
down
2018-08-08T13:08:53.673180+00:00 heroku[web.1]: Stopping all processes 
with SIGTERM
2018-08-08T13:08:53.753873+00:00 heroku[web.1]: Process exited with 
status 143
2018-08-08T17:26:03.449270+00:00 heroku[web.1]: Unidling
2018-08-08T17:26:03.449527+00:00 heroku[web.1]: State changed from down 
to starting
2018-08-08T17:26:06.709177+00:00 heroku[web.1]: Starting process with 
command `npm start`
2018-08-08T17:26:08.485322+00:00 app[web.1]: > gredictionary@1.0.0 start 
/app
2018-08-08T17:26:08.485299+00:00 app[web.1]: 
2018-08-08T17:26:08.485324+00:00 app[web.1]: > node app.js
2018-08-08T17:26:08.485325+00:00 app[web.1]: 
2018-08-08T17:26:09.807417+00:00 app[web.1]: Warning: connect.session() 
MemoryStore is not
2018-08-08T17:26:09.807458+00:00 app[web.1]: designed for a production 
environment, as it will leak
2018-08-08T17:26:09.807460+00:00 app[web.1]: memory, and will not scale 
past a single process.
2018-08-08T17:26:09.819650+00:00 app[web.1]: (node:20) 
DeprecationWarning: current URL string parser is deprecated, and will be 
removed in a future version. To use the new parser, pass option { 
useNewUrlParser: true } to MongoClient.connect.
2018-08-08T17:26:09.821394+00:00 app[web.1]: The web page has been 
started!
2018-08-08T17:26:10.414286+00:00 heroku[web.1]: State changed from 
starting to up
2018-08-08T17:26:11.540492+00:00 heroku[router]: at=info method=GET 
path="/" host=agile-everglades-19141.herokuapp.com request_id=afc6fb6e- 
aedd-4849-ae0b-33ab3eb7b2fa fwd="185.20.6.141" dyno=web.1 connect=0ms 
service=25ms status=200 bytes=4321 protocol=https
2018-08-08T17:49:02.244137+00:00 heroku[router]: at=info method=GET 
path="/" host=agile-everglades-19141.herokuapp.com request_id=27e6f629- 
ccb4-419d-ab36-4a10beb37205 fwd="131.107.174.241" dyno=web.1 connect=0ms 
service=4ms status=200 bytes=4323 protocol=https
2018-08-08T18:23:27.197045+00:00 heroku[web.1]: Idling
2018-08-08T18:23:27.197610+00:00 heroku[web.1]: State changed from up to 
down
2018-08-08T18:23:28.084450+00:00 heroku[web.1]: Stopping all processes 
with SIGTERM
2018-08-08T18:23:28.263192+00:00 heroku[web.1]: Process exited with 
status 143
2018-08-09T06:01:41.214783+00:00 heroku[web.1]: Unidling
2018-08-09T06:01:41.215047+00:00 heroku[web.1]: State changed from down 
to starting
2018-08-09T06:01:44.447331+00:00 heroku[web.1]: Starting process with 
command `npm start`
2018-08-09T06:01:46.278298+00:00 app[web.1]: 
2018-08-09T06:01:46.278311+00:00 app[web.1]: > gredictionary@1.0.0 start 
/app
2018-08-09T06:01:46.278313+00:00 app[web.1]: > node app.js
2018-08-09T06:01:46.278314+00:00 app[web.1]: 
2018-08-09T06:01:47.418611+00:00 app[web.1]: Warning: connect.session() 
MemoryStore is not
2018-08-09T06:01:47.418674+00:00 app[web.1]: designed for a production 
environment, as it will leak
2018-08-09T06:01:47.418676+00:00 app[web.1]: memory, and will not scale 
past a single process.
2018-08-09T06:01:47.430402+00:00 app[web.1]: (node:20) 
DeprecationWarning: current URL string parser is deprecated, and will be 
removed in a future version. To use the new parser, pass option { 
useNewUrlParser: true } to MongoClient.connect.
2018-08-09T06:01:47.432247+00:00 app[web.1]: The web page has been 
started!
2018-08-09T06:01:47.697504+00:00 heroku[web.1]: State changed from 
starting to up

标签: node.jsherokucommentsmlab

解决方案


推荐阅读