Javascript 返回函数Node.js的值
我有一个函数,可以从网页读取数据并将其发布到控制台中,但我想返回我已读取的数据并对其进行其他处理。因此,我不想返回console.log(温度),而是想返回温度值并执行console.log(下载(url)),但它发布“未定义”Javascript 返回函数Node.js的值,javascript,node.js,express,return,cheerio,Javascript,Node.js,Express,Return,Cheerio,我有一个函数,可以从网页读取数据并将其发布到控制台中,但我想返回我已读取的数据并对其进行其他处理。因此,我不想返回console.log(温度),而是想返回温度值并执行console.log(下载(url)),但它发布“未定义” //require https module var https=require("https"); //require cheerio to use jquery-like and return a DOM tree var cheerio=require('chee
//require https module
var https=require("https");
//require cheerio to use jquery-like and return a DOM tree
var cheerio=require('cheerio');
var global="";
//a function with url and callback par which connects to URL API and read the data
module.exports.download=function(url){
//read the data\
https.get(url,function(res){
var data="";
//add it to the data string
res.on('data',function(chunk){
data+=chunk;
});
//parse it with a callback function
res.on('end',function(){
var $=cheerio.load(data);
var temperature=$("span.temp.swip").text();
console.log(temperature);
});
}).on('error',function(err){
console.log(err.message)
});
}
//chose the url to connect
var Ploiesti='44.9417,26.0237';
var Brasov='45.597,25.5525';
var url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';
//download(url);
因此,您需要一个回调来将变量分配给它并从任何地方获取,请查看
cb
:
var download = function(url, cb){
//read the data\
https.get(url,function(res){
var data="";
//add it to the data string
res.on('data',function(chunk){
data+=chunk;
});
//parse it with a callback function
res.on('end',function(){
var $=cheerio.load(data);
var temperature=$("span.temp.swip").text();
cb(temperature);
});
}).on('error',function(err){
console.log(err.message)
});
}
module.exports.download = download;
然后,如果您需要从web调用该函数,您需要一个路由,请使用Express并将上一个文件另存为download.js
var express = require('express'),
app = express(),
download = require('download.js');
app.get('/temperature', function (req, res) {
// Get the temp
var Ploiesti='44.9417,26.0237',
Brasov='45.597,25.5525',
url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';
download.download(url, function (temp) {
// Send the response to the web
res.json({ temperature: temp);
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
因此,您需要一个回调来将变量分配给它并从任何地方获取,请查看
cb
:
var download = function(url, cb){
//read the data\
https.get(url,function(res){
var data="";
//add it to the data string
res.on('data',function(chunk){
data+=chunk;
});
//parse it with a callback function
res.on('end',function(){
var $=cheerio.load(data);
var temperature=$("span.temp.swip").text();
cb(temperature);
});
}).on('error',function(err){
console.log(err.message)
});
}
module.exports.download = download;
然后,如果您需要从web调用该函数,您需要一个路由,请使用Express并将上一个文件另存为download.js
var express = require('express'),
app = express(),
download = require('download.js');
app.get('/temperature', function (req, res) {
// Get the temp
var Ploiesti='44.9417,26.0237',
Brasov='45.597,25.5525',
url='https://darksky.net/forecast/' + Ploiesti + '/si24/en';
download.download(url, function (temp) {
// Send the response to the web
res.json({ temperature: temp);
});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
你必须在
res.on('end')
callback中这样做,我就是这样做的;我没有做console.log(温度),而是做了console.log(下载(url)),最后我做了console.log(下载(url))并打印出来。这类问题已经被问了数百次了。您必须学习如何在Javascript中使用异步结果进行编程。上面的副本为您提供了许多选项。您必须在res.on('end')
callback中执行此操作,这就是我的方法;我没有做console.log(温度),而是做了console.log(下载(url)),最后我做了console.log(下载(url))并打印出来。这类问题已经被问了数百次了。您必须学习如何在Javascript中使用异步结果进行编程。上面的副本为你提供了很多选择。好的答案除了你的代码外,还应该包含一个关于你如何解决OP问题的文本解释。这个答案没有提供任何解释,现在需要有人在你的代码和OP的代码之间做一个视觉差异,试图找出你改变了什么。是的,我做了这个,它工作了,但我不想在控制台上发布温度,我想将它保存到一个变量或对象,然后传递它并发布到网页上,所以我需要从该函数中提取可变温度如果在网页中需要,则需要为node.js建立路由,并从web下载要调用的函数。然后你可以用体温来回应电话。查看Express要做到这一点,请查看编辑后的答案我添加了您需要的所有内容。好的答案除了代码外,还应包含一个文本说明,说明您如何解决OP的问题。这个答案没有提供任何解释,现在需要有人在你的代码和OP的代码之间做一个视觉差异,试图找出你改变了什么。是的,我做了这个,它工作了,但我不想在控制台上发布温度,我想将它保存到一个变量或对象,然后传递它并发布到网页上,所以我需要从该函数中提取可变温度如果在网页中需要,则需要为node.js建立路由,并从web下载要调用的函数。然后你可以用体温来回应电话。查看Express要做这件事,请查看编辑后的答案,我添加了您需要的所有内容。