发送数组在module.exports的承诺 [英] send array in module.exports in promise

查看:139
本文介绍了发送数组在module.exports的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理合并swagger文件。我想传递数组,如

  [
{method:'1',path:'/ signIn',handler :funation(){returnabcd},
{method:'2',path:'/ signOut',handler:funation(){return123},
]

我根据我的需要得到结果,但没有传入module.exports,我已经检查到另一个文件控制台在需要这个文件后,
我的代码就像

  jsonResolve(root,options).then(function(results) {
const result = results.resolved.paths;
// console.log(result);
for(key in result){
// console.log('keys ',key);
const apiRoute = key;
const apiDescription = result [key];
let controller = apiDescription ['x-swagger-router-controller'];
for(method!=='x-swagger-router)($方法);
let newRoute = {};
if -controller'){
let operationId1 = apiDescription [method] .operationId;

newRoute = {
方法:方法
路径:apiRoute,
处理程序:controllers [controller] [operationId1](),
};
exoprtData.push(newRoute);
}
}
}
return exoprtData;
})
.then((result)=> {
console.log(result);
module.exports = result;
})

我已经将结果检查到最后。但没有传入module.exports



我的需求文件代码就像,

  var ApiRoutes = require(./ routes / api); 
console.log(ApiRoutes,ApiRoutes);

这里我得到结果到控制台,如

  ApiRoutes {} 

请帮我解决这个问题。感谢提前

解决方案

Node.js期望任何模块同步加载 - 即。 必须分配给当前执行循环中的 module.exports ,否则模块的导出内容将是默认的空对象。



在解决承诺后,您将分配给 module.exports 这将无法正常工作 - 在承诺解决之后,Node.js已经完成加载模块并将导出的值(空对象)缓存为模块的内容。您现在不能通过分配到 module.exports 来更改它。



要解决此问题,您必须: / p>


  • 导出通过promise返回数组的函数

  • 不知何故使它同步工作(取决于什么 c code code code code code code code code code <


I am working on merge swagger files. I want to pass array like

[ 
  { method: '1', path: '/signIn', handler: funation() {return "abcd"},
  { method: '2', path: '/signOut', handler: funation() {return "123"},
]

I get result as per my need but it was not pass into module.exports, i have check it into another file console after require this file, my code is like

jsonResolve(root, options).then(function (results) {
  const result = results.resolved.paths;
  // console.log(result);
  for (key in result) {
    // console.log('keys', key);
    const apiRoute = key;
    const apiDescription = result[key];
    let controller = apiDescription['x-swagger-router-controller'];
    for (method in apiDescription) {
      // console.log("methods", method);
      let newRoute = {};
      if (method !== 'x-swagger-router-controller') {
        let operationId1 = apiDescription[method].operationId;

        newRoute = {
          method: method,
          path: apiRoute,
          handler: controllers[controller][operationId1](),
        };
        exoprtData.push(newRoute);
      }
    }
  }
  return exoprtData;
})
.then((result) => {
  console.log(result);
  module.exports = result;
})

I had check it result into last then. but not pass into module.exports

my require file code is like,

var ApiRoutes = require("./routes/api");
console.log("ApiRoutes", ApiRoutes);

And here i get result into console like

ApiRoutes {}

Please help me to solve this problem. Thanks in advance

解决方案

Node.js expects any module to be loaded synchronously - ie. you must assign to module.exports in the current execution loop, otherwise the module's exported content will be a default empty object.

You are assigning to module.exports after you resolve a promise. This will not work - by the time the promise is resolved, Node.js already finished loading your module and cached the exported value (empty object) as the module's contents. You cannot change it by assigning to module.exports now.

To solve this problem, you must either:

  • export a function which returns the array through promise
  • Somehow make it work synchronously (depends on what jsonResolve() does)
  • Assign the actual promise to module.exports

这篇关于发送数组在module.exports的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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