无法在Javascript中传递变量

时间:2014-02-05 20:08:46

标签: javascript node.js

我正在尝试使用node.js从word文档中抓取数据。

我目前的问题是,下面的控制台日志会将juice块中的值作为适当的变量返回。如果我把它移到果汁块外面就会完全丢失。我试过把回归

function getMargin(id, content){

    var newMargin = content.css("margin-left");

    if(newMargin === undefined){
        var htmlOfTarget = content.toString(),
            whereToCut = theRaw.indexOf("<div class=WordSection1>");

        fs.writeFile("bin/temp/temp_" + id + ".htm", theRaw.slice(0, whereToCut) + htmlOfTarget + "</body> </html>", function (err){
            if (err) {
                throw err;
            }
        });

        juice("bin/temp/temp_" + id + ".htm", function (err, html) {
            if (err) {
                throw err;
            }
            var innerLoad = cheerio.load(html);
            newMargin = innerLoad("p").css("margin-left");
            console.log(newMargin); // THIS newMargin AS VALUE 
        });
    }
    console.log(newMargin);//THIS RETURNS newMargin UNDEFINED
    return newMargin;
}

我认为问题在于fs.write和juice是Asyc函数。我根本不知道如何绕过它。我必须能够按顺序在某些点调用getMargin。

1 个答案:

答案 0 :(得分:2)

如评论中所述,在异步代码完成后,将程序流程更改为在回调中运行...

// accept callback as parameter, and run it after async methods complete...
function getMargin(id, content, callback){

    var newMargin = content.css("margin-left");

    if(newMargin === undefined){
        var htmlOfTarget = content.toString(),
            whereToCut = theRaw.indexOf("<div class=WordSection1>");

        fs.writeFile("bin/temp/temp_" + id + ".htm", theRaw.slice(0, whereToCut) + htmlOfTarget + "</body> </html>", function (err){
            if (err) {
                throw err;
            }

            // move the juice call inside the callback of the file write operation
            juice("bin/temp/temp_" + id + ".htm", function (err, html) {
                if (err) {
                    throw err;
                }
                var innerLoad = cheerio.load(html);
                newMargin = innerLoad("p").css("margin-left");
                console.log(newMargin); // THIS newMargin AS VALUE 

                // now run the callback passed in the beginning...
                callback();

            });

        });

    }
}


// call getMargin with callback to run once complete...
getMargin("myId", "myContent", function(){
    // continue program execution in here....
});