插入 mongo 数据库时出现 504 错误 [英] 504 error when inserting into mongo database

查看:58
本文介绍了插入 mongo 数据库时出现 504 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将passportjs 与nodejs 一起使用时,尝试插入数据库时​​出现504 错误.

When using passportjs with nodejs, I get a 504 error when trying to insert into the database.

mongo 数据库托管在应用程序服务器外部,应用程序位于反向代理之后(反向代理将应用程序端口转换为端口 80).

The mongo database is hosted externally from the application server and the application is behind a reverse proxy (the reverse proxy converts the application port to port 80).

此问题仅在尝试插入 mongo 文档时发生:

The issue only happens when trying to insert into a mongo document:

passport.use(new GoogleStrategy({
    clientID: "client_id",
    clientSecret: "client_secret",
    callbackURL: "oauth_url_callback"
  },
  function(accessToken, refreshToken, profile, done) {
    passport.serializeUser(function(user,done){
      mongo.connect(url, function(err,db){
        if(err){
          console.log(err, ' was the err');

          fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
            fs.writeFileSync('views/debug/debug.txt', err);
          });

          done(err);
        } else {
          db.collection('users').find({profile:profile}, function(err, user) {
            if (err) {
              fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
                fs.writeFileSync('views/debug/debug.txt', err);
              });

              db.collection('users').insertOne({profile:profile, name:"John Doe"}, function(err, user){
                if(err){
                  console.log("failed to insert", err);
                } else {
                  console.log('Registered a new user!');
                }
              });
            } else {
              console.log('We have a returning user!');
            }

            console.log('testing');

            fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
              fs.writeFileSync('views/debug/debug.txt', err);
            });

            done(null, user);
          });
        }
      });
    });
  }
));

我不确定为什么会发生这种情况,但只有在您尝试插入数据库时​​才会发生.当我刷新页面时,它不是 504,因为它返回一个错误,指出令牌已过期.

I am not sure why this happens, but it only happens when you try to insert into the database. When I refresh the page, it doesn't 504 because it returns a error saying the token has expired.

我也尝试记录错误,似乎没有错误.我以 stdout 和 stderror 作为参数开始该过程以提取输出但无济于事.

I also tried logging for errors, and there seems to be no errors. I start the process with both stdout and stderror as arguments to extract output but to no avail.

它也没有将文档/项目插入到数据库中,这很奇怪.

It also doesn't insert the document/item into the db which is strange.

推荐答案

结果是我重载了 done 函数,在两个回调中定义了两次.我分别定义并分别调用它们.

It turns out I was overloading the done function be defining it twice in both callbacks. I defined each separately and called them separately.

这篇关于插入 mongo 数据库时出现 504 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆