Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

node.js - 504 error when inserting into mongo database

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...