search

Home  >  Q&A  >  body text

javascript - How to transfer values ​​between different modules in node.js


data.js (get a json, the console comes out correctly)

Pass it to index.js through exports, and then pass it to the index.jade template. It turns out that the getdata is an empty object. As a result, title is also an empty object. If getdata.title is displayed, undefined is displayed. Converting json to a string is also an empty string. What is the reason? Is it caused by asynchronous io?

我想大声告诉你我想大声告诉你2755 days ago1003

reply all(3)I'll reply

  • PHPz

    PHPz2017-05-16 13:25:42

    Give me an idea promise 或者 callback (回调函数)

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:25:42

    exports=json, exports is now a reference to the json object, no longer a reference to module.exports, so json is not linked to module.exports, so you can either module.exports=json, then require or exports. json=json and then get the value of the attribute json of the object required by this module

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:25:42

    You can encapsulate data.js into a module, and then execute the render method as a callback; or use promise to execute the render method in the then logic. Your current writing method cannot obtain the asynchronous value. Secondly, the way you export json is wrong, module.exports=json.

    reply
    0
  • Cancelreply