最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何从函数返回值以使用它并在Node.js的路由器中重新启用它?

IT培训 admin 5浏览 0评论

如何从函数返回值以使用它并在Node.js的路由器中重新启用它?

我正在尝试从Nodejs中的getWeather()函数获取结果,以在一台路由器中将其重新生成为json,但我无法获取。

var request = require('request');
var publicIp = require('public-ip');

function getCity (userip){ 
  var url = `/${userip}/json`;
  request(url, (err, respone, body)=>{
    var data = JSON.parse(body);
    var city = data['city'];
    return getLocationKey(city);
  })
}

function getLocationKey(city){
  var url = `=${city}&apikey=${API_KEY}`;
  request(url, (err, respone, body)=>{
    var data = JSON.parse(body);
    var key = data[0].Key;
    return getWeather(key);
  })
}

function getWeather(key){
  var url =  `/${key}?apikey=${API_KEY}`;  
  request(url, (err, respone, body)=>{
      var weather = JSON.parse(body);
      console.log("weather: " + weather);
      return weather;
  })
}

我从getCity()和getLocationKey()获得了结果,但是从getWeather()获得最终结果却不成功。我的console.log天气是Object对象。我尝试将其分开并仅调用它,它对我产生了天气的影响images

router.get('/weather-weather', (req, res)=>{
  var city = 'hanoi';
  var key = '353412'
  var url =  `/${key}?apikey=${API_KEY}`;
  request(url, (err, respone, body)=>{
    var weatherDetails = JSON.parse(body);
    res.json(weatherDetails);
  })
})

但是,我想在此路由中调用它以重新生成json,但是失败了

router.get('/weather', (req, res)=>{
  publicIp.v4()
  .then(userip=>{
    console.log("userIP: " + userip); 
    getCity(userip);
  })
  .catch(err=>{ 
    console.log('Error: '+ err);
  })
  })

但是失败了。我不知道如何从getWeather()函数返回响应结果。我如何得到它?

回答如下:

函数getXXX无法获得回调函数内部的“ return”。

并且您没有调用res.json将结果发送到客户端。

您可以将res传递给getXXX并以这种方式使用它:

function getCity(userip, res) {
  var url = `https://ipinfo.io/${userip}/json`;
  request(url, (err, respone, body) => {
    var data = JSON.parse(body);
    var city = data['city'];
    return getLocationKey(city, res);
  })
}

function getLocationKey(city, res) {
  var url = `http://dataservice.accuweather/locations/v1/cities/search?q=${city}&apikey=${API_KEY}`;
  request(url, (err, respone, body) => {
    var data = JSON.parse(body);
    var key = data[0].Key;
    return getWeather(key, res);
  })
}

function getWeather(key, res) {
  var url = `http://dataservice.accuweather/forecasts/v1/daily/1day/${key}?apikey=${API_KEY}`;
  request(url, (err, respone, body) => {
    var weather = JSON.parse(body);
    console.log("weather: " + weather);
    res.json(weather);
  })
}

router.get('/weather', (req, res) => {
  publicIp.v4()
    .then(userip => {
      getCity(userip, res);
    })
    .catch(err => {
      console.log('Error: ' + err);
    })
})

如何从函数返回值以使用它并在Node.js的路由器中重新启用它?

我正在尝试从Nodejs中的getWeather()函数获取结果,以在一台路由器中将其重新生成为json,但我无法获取。

var request = require('request');
var publicIp = require('public-ip');

function getCity (userip){ 
  var url = `/${userip}/json`;
  request(url, (err, respone, body)=>{
    var data = JSON.parse(body);
    var city = data['city'];
    return getLocationKey(city);
  })
}

function getLocationKey(city){
  var url = `=${city}&apikey=${API_KEY}`;
  request(url, (err, respone, body)=>{
    var data = JSON.parse(body);
    var key = data[0].Key;
    return getWeather(key);
  })
}

function getWeather(key){
  var url =  `/${key}?apikey=${API_KEY}`;  
  request(url, (err, respone, body)=>{
      var weather = JSON.parse(body);
      console.log("weather: " + weather);
      return weather;
  })
}

我从getCity()和getLocationKey()获得了结果,但是从getWeather()获得最终结果却不成功。我的console.log天气是Object对象。我尝试将其分开并仅调用它,它对我产生了天气的影响images

router.get('/weather-weather', (req, res)=>{
  var city = 'hanoi';
  var key = '353412'
  var url =  `/${key}?apikey=${API_KEY}`;
  request(url, (err, respone, body)=>{
    var weatherDetails = JSON.parse(body);
    res.json(weatherDetails);
  })
})

但是,我想在此路由中调用它以重新生成json,但是失败了

router.get('/weather', (req, res)=>{
  publicIp.v4()
  .then(userip=>{
    console.log("userIP: " + userip); 
    getCity(userip);
  })
  .catch(err=>{ 
    console.log('Error: '+ err);
  })
  })

但是失败了。我不知道如何从getWeather()函数返回响应结果。我如何得到它?

回答如下:

函数getXXX无法获得回调函数内部的“ return”。

并且您没有调用res.json将结果发送到客户端。

您可以将res传递给getXXX并以这种方式使用它:

function getCity(userip, res) {
  var url = `https://ipinfo.io/${userip}/json`;
  request(url, (err, respone, body) => {
    var data = JSON.parse(body);
    var city = data['city'];
    return getLocationKey(city, res);
  })
}

function getLocationKey(city, res) {
  var url = `http://dataservice.accuweather/locations/v1/cities/search?q=${city}&apikey=${API_KEY}`;
  request(url, (err, respone, body) => {
    var data = JSON.parse(body);
    var key = data[0].Key;
    return getWeather(key, res);
  })
}

function getWeather(key, res) {
  var url = `http://dataservice.accuweather/forecasts/v1/daily/1day/${key}?apikey=${API_KEY}`;
  request(url, (err, respone, body) => {
    var weather = JSON.parse(body);
    console.log("weather: " + weather);
    res.json(weather);
  })
}

router.get('/weather', (req, res) => {
  publicIp.v4()
    .then(userip => {
      getCity(userip, res);
    })
    .catch(err => {
      console.log('Error: ' + err);
    })
})
发布评论

评论列表 (0)

  1. 暂无评论