简体   繁体   English

将参数传递给Node.js中的导入函数

[英]Passing parameters to imported function in Node.js

I feel like this shouldn't be hard, but I'm new to Node.js (and also new to many aspects of JavaScript). 我觉得这应该不难,但是我是Node.js的新手(也是JavaScript的许多方面的新手)。 I'm creating an Express application to return arrays of bike locations that are fetched from multiple APIs; 我正在创建一个Express应用程序,以返回从多个API获取的自行车位置数组; each API requires the longitude and latitude as inputs. 每个API都需要经度和纬度作为输入。 So I've broken each API call into a 'module' and I'm using the npm library 'async' to make each call in parallel, and using 'axios' to make the API requests. 因此,我已将每个API调用分解为一个“模块”,并使用npm库“ async”并行进行每个调用,并使用“ axios”进行API请求。 I can get it to work just fine without breaking it up into modules, but once I separate each API call into its own file, I can't figure out how to pass lat and lng into it. 我可以使它正常工作而无需将其分解为模块,但是一旦将每个API调用分离到自己的文件中,就无法弄清楚如何将lat和lng传递到其中。

Here is my index.js 这是我的index.js

import async from 'async';
import {mobike} from './mobike.js';
import {spin} from './spin.js';

async.parallel([
    mobike, //How or where can I pass these parameters?
    spin
],
function(err, results) {
    console.log(results);
});

and here is my mobike.js module, for example (I'll omit spin.js for brevity) 这是我的mobike.js模块,例如(为简便起见,我将省略spin.js)

import axios from 'axios';

export var mobike = function (lat, lng, callback){
  axios.get('https://mwx.mobike.com/mobike-api/rent/nearbyBikesInfo.do', {
    params: {
      latitude: lat, //35.2286324
      longitude: lng //-80.8427562
    },
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      'user-agent': 'Mozilla/5.0'
    }
  }).then( response => {
      callback(null, response.data.object)
  }).catch(error => {
      callback(error, null)
  })
}

When I try and pass arguments through mobike (like mobike(1234,1234) ), it doesn't work. 当我尝试通过mobike传递参数时(例如mobike(1234,1234) ),它不起作用。 How can I pass the lat and lng arguments to the mobike.js file? 如何将lat和lng参数传递给mobike.js文件?

For the parallel method each function only has a callback parameter. 对于并行方法,每个函数仅具有一个回调参数。

The easiest way to achieve what you want is like this 实现您想要的最简单的方法是这样的

async.parallel([
    function (callback) {
        var lng = 0.0
        var lat = 0.0
        mobike(lat, lng, callback), //This is where you pass in parameters
    },
    spin
],
function(err, results) {
    console.log(results);
});

From a tutorial I found online , it looks like you need to wrap your mobike and spin functions inside other functions in order to bind in your data along with the callback function provided by the async module. 我在线上找到的教程中 ,您似乎需要将mobikespin函数包装在其他函数中,以便将数据与async模块提供的回调函数绑定在一起。 Something like this: 像这样:

var lat = 35.2286324;
var lng = -80.8427562;

async.parallel([
    function(callback) { mobike(lat, lng, callback); },
    function(callback) { spin(lat, lng, callback); },
],
function(err, results) {
    console.log(results);
});

You can also use bind to pass parameters. 您还可以使用bind来传递参数。 Example: 例:

const async = require('async')

const sleep = (ms = 0) => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

async.parallel([
    mobike.bind(null, 1000), //How or where can I pass these parameters?
    spin.bind(null, 500)
],
function(err, results) {
    console.log(results);
})

async function mobike (ms) {
  await sleep(ms) // pretend to wait for an api request
  console.log('mobike waited for ' + ms + ' ms')
  return ms
}

async function spin (ms) {
  await sleep(ms) // pretend to wait for an api request
  console.log('spin waited for ' + ms + ' ms')
  return ms
}

Results: 结果:

spin waited for 500 ms
mobike waited for 1000 ms
[ 1000, 500 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM