Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中一个函数返回两个函数-NodeJS和ExpressJS_Javascript_Node.js_Express - Fatal编程技术网

如何在Javascript中一个函数返回两个函数-NodeJS和ExpressJS

如何在Javascript中一个函数返回两个函数-NodeJS和ExpressJS,javascript,node.js,express,Javascript,Node.js,Express,我遇到了一个问题,我必须组合这些对象并在一个函数中返回。问题是这两个对象的属性几乎相同,但值不同 所以我创建了两个分离的函数,每个函数都有一个相同的对象,几乎相同的道具,diff值。 在每个函数中,我都有一个url,用于两种不同的情况:case1=skoda,case2=toyota 问题是,我不知道如何进行这些diff-get调用(我在那里放了一条注释),以便在一个函数中返回两个car,并在一个xml链接中列出。现在每辆车都在一个单独的xml链接上,因为有两个单独的文件。现在只有一个链接,我不

我遇到了一个问题,我必须组合这些对象并在一个函数中返回。问题是这两个对象的属性几乎相同,但值不同

所以我创建了两个分离的函数,每个函数都有一个相同的对象,几乎相同的道具,diff值。 在每个函数中,我都有一个url,用于两种不同的情况:case1=skoda,case2=toyota

问题是,我不知道如何进行这些diff-get调用(我在那里放了一条注释),以便在一个函数中返回两个car,并在一个xml链接中列出。现在每辆车都在一个单独的xml链接上,因为有两个单独的文件。现在只有一个链接,我不知道怎么做

有人能帮我一点忙吗

const env = process.env.NODE_ENV || 'development';
const apiUrl = config.api[env];

  const skodaParams = {
    categoryId: 'skoda',
    limit: 1000,
    offset: 0
  };

  const toyotaParams = {
    categoryId: 'toyota',
    limit: 1000,
    offset: 0
  };

const generateSkoda = skoda => {
  // a lot of code for skoda prop
  const skodaList = {
    'g:id': product.carId
    'g:brand': product.brand,
    'g:color': product.color
    'g:teamSkoda: 'Skoda'
  }

  return skodaList;
}

const generateToyota = toyota => {
  // a lot of code for toyota prop
  const skodaList = {
    'g:id': product.carId
    'g:brand': product.brand,
    'g:color': product.color,
    'g:teamToyota: 'Toyota'
  }

  return skodaList;
}

const getSkoda = () => {
  return axios.get(apiUrl, { params: skodaParams });
}
const getToyota = () => {
  return axios.get(apiUrl, { params: toyotaParams });
}


const createBothCars = () => { 

  // here I wanted to add some how two axios get calls for both skoda and toyota
  return axios
    .all([getSkoda(), getToyota()])
    .then(response => {
      console.log('response', response);

      const skodaArray = [];

      response.data.skoda.map(item => {
        return skodaArray.push(generateSkoda(item));
      })

      const Obj = {
        feed: {
          $: {
            xmlns: 'http://www.w3.org/2005/Atom',
            'xmlns:g': 'http://base.google.com/ns/1.0'
          },
          title: 'Skoda cars list',
          link: {
            $: {
              rel: 'self',
              href: config.domain
            }
          },
          entry: skodaArray
        }
      };

      const objBuilder = new xml2js.Builder({
        cdata: true
      });

      return objBuilder.buildObject(Obj);
    })
    .catch(error => {
      console.log(error);
    });
};

module.exports = createXml;
在server.js中,我有:

const createXml = require('../src/xml/createXml');

app
  .prepare()
  .then(() => {
    const server = express();

server.get('/both-cars.xml', (req, res) => {
      res.header('Content-Type', 'application/xml');
      (async function sendXML() {
        const xmlFile = await createXml();
        res.send(xmlFile);
      })();
    });

更新

我在注释之后用axios.all更新了代码,但在响应中,我没有从两个函数中获取值,而是从该数组中列出的最后一个函数中获取值:

[getSkoda(),getToyota()]

现在我只从getToyota获得值

data: {
      filters: [Array],
      skoda: [],
      toyota: [Array],
      _version: 'Fri Jun 19 14:43:47 EEST 2020',
      errorCode: 'no_error'
    }
如果我切换顺序[getToyota(),getSkoda()],我将只从getSkoda获取值

这来自console.log:

data: {
      filters: [Array],
      skoda: [Array],
      toyota: null,
      _version: 'Fri Jun 19 14:47:09 EEST 2020',
      errorCode: 'no_error'
    }

任何其他帮助?

作为响应,您应该收到两个值:

return axios
.all([getSkoda(), getToyota()])
.then((responseOne, responseTwo) => {
  console.log('responseOne', responseOne);
  console.log('responseOne', responseOne);
下面的示例代码可能有助于进一步:

axios.all([getToyota(), getSkoda()])
  .then(axios.spread(function (toyotaResponse, skodaResponse) {
    // Both requests are now complete
  console.log('toyota Response', toyotaResponse);
  console.log('skoda Response', skodaResponse);
  }))

)

对于多个并发请求,您可以使用axios.all(),请查看位于的文档,这将对您有所帮助further@Manoj我更新了密码。如果你现在能看一下描述,我展示了我得到的。有一个问题,我无法从这两个函数中获取值。请检查答案中的代码,我可能会进一步提供帮助。。