简体   繁体   中英

Unexpected token function in node callback

I'm really new to node and callbacks. I'm working on a Nodeschool problem: to return the data of three given URLs in order using callbacks. I put the results of the three URLs in an object, to keep track of them in order. However I keep failing the tests because, I think, the URL object is being returned before I put all the URLs in it.

TLDR:

Here's the offending function. It keeps telling me "SyntaxError: Unexpected token function." But when I put console.log(urlObj[url]) into its own function and called it by name in storeUrl , I don't think it was run after the if statements...

function storeUrl(url, info, function(url) {
    // Only log after all the checking is done
    console.log(urlObj[url]);
} ){
// add data to a dict? or something? 
// to keep track of them???
if(url == url1){
    urlObj[url1] = info;
}

if(url == url2){
    urlObj[url2] = info;
}

if(url == url3){
    urlObj[url3] = info;
}

}

Here is my entire file:

var http = require('http');
var bl = require('bl');
var url1 = process.argv[2];
var url2 = process.argv[3];
var url3 = process.argv[4];
var urlObj = {};
urlObj[url1] = '';
urlObj[url2] = '';
urlObj[url3] = '';


function firstUrl(callback, callback){
    getHttp(url1);
    callback(url2);
    callback(url3);
}

function startItOff(){
    firstUrl(function secondUrl(contents){
    getHttp(contents);
    }, function thirdUrl(contents){
    getHttp(contents);
    });

    // This is what I ultimately want: 
    // A log of the data of each URL, in order, on its own line:
    for (item in urlObj){
        console.log(urlObj[item]);
    }
}

//////////////
// Above this line is ok,
// now trying to retrieve the url only after 
// getting the right one. The getHttp shows
// them in the right order, but retrieveHttp
// doesn't have an order to it.
/////////////

function getHttp(url){

    middleman(url, retrieveHttp);

}

function middleman(url, callback){
    callback(url);
}

function retrieveHttp(url){


    http.get(url, function(res){

    res.setEncoding('utf8');
    res.pipe(bl(function(err,data){
        if (err) return Error('Error');
        storeUrl(url, data.toString(), callback);

    }));

    });
}

function storeUrl(url, info, function(url) {
    console.log(urlObj[url]);
} ){
    // add data to a dict? or something? 
    // to keep track of them???
    if(url == url1){
    urlObj[url1] = info;
    }

    if(url == url2){
    urlObj[url2] = info;
    }

    if(url == url3){
    urlObj[url3] = info;
    }

    // Also trying to log the data from the URLs here:
    console.log(urlObj[url1]);

}

startItOff();

This line is a syntax error:

function storeUrl(url, info, function(url) {
// Only log after all the checking is done -->

So when Node says its a syntax error, its not lying :). When you are defining the arguments to a function which is what you are doing there, you can't include the word function.

This would be valid syntax:

function storeUrl(url, info, callback) {
  console.log(url);
  callback();
}

I recommend not trying to learn Node and Javascript at the same time. Learn Javascript basics and debugging, then callbacks, then tackle Node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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