当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Issue exporting module NodeJS

问题描述:

I have this directory structure:

  • app/router.js
  • app/oauth2-home-client/oauth2-client.js

And the sources:

app/oauth2-home-client/oauth2-client.js

 //SOME CODE

exports.Bearer = {

authenticate : passport.authenticate('bearer', { session : false }),

initialize : passport.initialize()

// session : passport.session()

};

app/router.js

var oauth2 = require('./oauth2-home-client/oauth2-client');

console.log(JSON.stringify(oauth2.Bearer));

//SOME CODE

When I print oauth2.Bearer (and oauth2, too) content, I get {}.

What am I doing wrong?

Thanks.

网友答案:

Your code:

exports.Bearer = {
  authenticate : passport.authenticate('bearer', { session : false }),
  initialize : passport.initialize()
  // session : passport.session()
};

Will result in:

exports.Bearer = {
  authenticate :undefined,
  initialize : undefined
};

because both passport.authenticate and passport.initialize return undefined.

And the keys having the value undefined are omitted by JSON.stringify.

[...]If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).[...]

网友答案:

Its value may point to the module instantiation. Have you try this?

module.exports = {...};
分享给朋友:
您可能感兴趣的文章:
随机阅读: