简体   繁体   English

Node.js + Actionhero代码未连续运行

[英]Node.js + Actionhero code not running consecutively

I'm obviously missing something very important about how Node.js and Actionhero work. 我显然缺少关于Node.js和Actionhero的工作方式非常重要的东西。

Can someone please explain to me why the following problem occurs: 有人可以向我解释为什么会出现以下问题:

I have this simple code: Basically it reads a .css file . 我有这个简单的代码:基本上,它读取一个.css文件。

buildAdsHTML: function (ads, type) {
        var cssDir = path.normalize(__dirname + '/../public/css/');
        switch (type) {
            case  'text':
                var result = '';
                console.log("STEP 1");
                fs.readFile(cssDir + type + 'Ads.css', {encoding: 'utf8'}, function (err, data) {

                    console.log(err);
                    console.log(data);
                    if (!err) {

                        console.log("STEP 2");
                        result += '<style>' + data + '</style>';

                    } else {

                    }
                });

                break;

        }
        console.log('STEP 3');
        return result;
    }

Now when this code is run I get an empty string as result. 现在,当运行此代码时,我得到一个空字符串。 The console output is the following: 控制台输出如下:

STEP 1 <- so far so good 
STEP 3<- this should be last!

null <- this is the error variable 

.some-random-css-class{
    display: block;
    width: 100;
    min-height: 250px;
}

STEP 2

Now at some point I figured out that that fs.reafFile is async function. 现在,我发现fs.reafFile是异步函数。 So I naturaly changed it to the sync version fs.readFileSync . 因此,我自然将其更改为同步版本fs.readFileSync Now the console output is even worse: 现在控制台输出更糟:

STEP 1
STEP 3

Thats it! 而已! Nothing else. 没有其他的。 And I still get a empty string as result. 而且我仍然得到一个空字符串。 Like the whole code isnt even going through the swich . 就像整个代码一样,甚至都swich通过。

I've noticed this behavior in all functions and methods of my actionhero project, most notebly when calling next(connection) . 我在actionhero项目的所有功能和方法中都注意到了这种行为,最值得注意的是在调用next(connection) You can't just call it at the end of the method. 您不能只在方法末尾调用它。

For every if or swich I have to call it inside to have any actual control over the result. 对于每个ifswich我必须在内部调用它以对结果进行任何实际控制。

Otherwise every new data I've added to the connection is lost. 否则,我添加到连接中的所有新数据都会丢失。

What's with that ? 那是什么 Please explain that functionality in detail so I don't make any dumn mistakes while coding. 请详细说明该功能,以便在编码时我不会犯任何愚蠢的错误。

Finaly figured it out. 菲纳丽想通了。

The problem was that I didnt notice that fs.readFileSync had no callback funtction. 问题是我没有注意到fs.readFileSync没有回调功能。

So the proper use for that function should have been 因此,应正确使用该功能

var result= fs.readFileSync(pathToFile,{options})
return result;

I can still use some help on the problem i mentiond with next(connection); 对于next(connection);提到的问题,我仍然可以使用一些帮助next(connection); . I dont realy know why it skips whole parts of code like that. 我真的不知道为什么它会跳过整个代码部分。

I've found next() doesn't like it if you pass it any parameters. 我发现如果您将参数传递给它,next()会不喜欢它。 I had a bug in my application for a good day or two because I was doing '.then(next);' 我的应用程序中存在一个错误,长达一两天,因为我在做“ .then(next);”。

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

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