Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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).

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);
          });
        }
      });
    });
  }
));

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
582 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...