Javascript issue - Too many errors

已查看 730 次
跳至第一个未读帖子

Walter Kraembring

未读,
2016年6月30日 01:51:362016/6/30
收件人 Node-RED
Hello,

I would need some advice if someone could be kind and have a look? In my flow I have a function node with the javascript code below. At line 128 the editor indicates an error, "Too many errors found..."  

The problem...the code is working...and I cannot see what is wrong. If cosmetic, fine, but if I have a bug somewhere, I would like to fix it

Kind regards, Walter




// initialise the variables if they doesn't exist already
context.set('latestMode',msg.payload);
context.set('nIntervId',context.get('nIntervId')||null);
context.set('lastMessages',context.get('lastMessages')||null);
 
var CHECK_PERIOD = 2000; // milliseconds
clearInterval(context.get('nIntervId'));

var sI = setInterval(function() {
    var msg1 = "";
    var msg2 = "";
    var msg3 = "";
    var msg4 = "";
    var msg5 = "";
    var msg6 = "";
    var msg7 = "";
    var msg8 = "";
    var msg9 = "";
    
    var condition = context.get('latestMode');
    var hday = '';
    
    var d = new Date();
    var wday = d.getDay();
    var month = d.getMonth()+1;
    if (month < 10) {
        month = '0'+ month;
    }
    var day = d.getDate();
    if (day < 10) {
        day = '0'+ day;
    }
    
    d.setDate(d.getDate() + 1);
    var month_tomorrow = d.getMonth()+1;
    if (month_tomorrow < 10) {
        month_tomorrow = '0'+ month_tomorrow;
    }
    var day_tomorrow = d.getDate();
    if (day_tomorrow < 10) {
        day_tomorrow = '0'+ day_tomorrow;
    }
    
    var td = month+day;
    var tm = month_tomorrow+day_tomorrow;
    
    if (global.get('myHolidays').indexOf(tm)>-1 && global.get('myHolidays').indexOf(td)==-1){
        condition = 'holiday_tomorrow';
    }
    if (global.get('myHolidays').indexOf(tm)>-1 && global.get('myHolidays').indexOf(td)>-1){
        condition = 'holiday_today';
    }
    if (global.get('myHolidays').indexOf(tm)==-1 && global.get('myHolidays').indexOf(td)>-1){
        if(wday < 5){
            condition = 'holiday_ends';
        }
        hday = 'h_ends';
    }
    
    switch (condition) {
        case 'holiday_today':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"auto" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'Holiday today';
        myMsg = 0;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'holiday_tomorrow':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"auto" };
        msg9 = { payload:"disabled" };
        hday = 'Holiday tomorrow';
        myMsg = 1;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'holiday_ends':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"auto" };
        hday = 'Holiday ends today';
        myMsg = 2;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'auto':
        msg1 = { payload:"auto" };
        msg2 = { payload:"auto" };
        msg3 = { payload:"auto" };
        msg4 = { payload:"auto" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        if(hday == 'h_ends'){
            hday = 'Auto(Holiday Ends)';
        }
        else{
            hday = 'Automatic';
        }
        myMsg = 3;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'on':
        msg1 = { payload:"on" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'On';
        myMsg = 4;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'off':
        msg1 = { payload:"off" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'Off';
        myMsg = 5;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'disabled':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'Disabled';
        myMsg = 6;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'vacation':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"auto" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'Vacation';
        myMsg = 7;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
        case 'away':
        msg1 = { payload:"disabled" };
        msg2 = { payload:"disabled" };
        msg3 = { payload:"disabled" };
        msg4 = { payload:"disabled" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"auto" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
        hday = 'Away';
        myMsg = 8;
        if(myMsg !== context.get('lastMessages')){
            node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);
            context.set('lastMessages',myMsg);
        } 
        break;
    }
    node.status({
    text : hday
    });
}, CHECK_PERIOD);

context.set('nIntervId',sI);




Julian Knight

未读,
2016年6月30日 04:22:202016/6/30
收件人 Node-RED
Can't see much obvious there. You've a few places where you have used a == test instead of === and quite a few places where you have ' or " for string delimiters but nothing obviously wrong.

I think I'd suggest that you paste the code into a JavaScript linter so you can see any hidden issues.

Nicholas O'Leary

未读,
2016年6月30日 04:44:272016/6/30
收件人 Node-RED Mailing List
Not immediately sure what's going on. If you add the following to the top of your function it will hide the 'Too many errors' message as it increases the maximum number jshint will find before bailing out.
  /*jshint maxerr: 100000 */

That said, I had expected adding that to allow jshint to finishing linting your code and then display the errors it hand found - but it doesn't show anything wrong.

Some more investigation needed...

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

dave....@gmail.com

未读,
2016年6月30日 06:24:592016/6/30
收件人 Node-RED
Im pretty dumb but don't you need to setup var myMsg = {};

Julian Knight

未读,
2016年6月30日 06:49:012016/6/30
收件人 Node-RED
Urg! It hadn't occurred to me that you could put a jshint statement in the editor B-}

I like that! Now I can write function nodes in my preferred JS style! (mainly without the pointless semi-colons everywhere).

Walter Kraembring

未读,
2016年6月30日 06:58:132016/6/30
收件人 Node-RED
Im pretty dumb but don't you need to setup var myMsg = {}

Maybe I selected a stupid variable name for it...myMsg is just an integer helping me (or the code) to remember last messages sent 

BR Walter

Walter Kraembring

未读,
2016年6月30日 07:00:142016/6/30
收件人 Node-RED
Dear Julian And Nick,

Thank you, I cleaned up a bit (using only ' for string delimiters and consistently !== and === for comparisons) and then pasted the code and checked it with an online lint tool

Before making the changes, I got a warning, a default case was missing in my switch, but no complaints about the rest

Still, in Node-RED, the same warning appears after all changes made (Nick's advice did of course suppress the warning but is this a good practice??) 

This is my cleaned up code:

    if (global.get('myHolidays').indexOf(tm)>-1 && global.get('myHolidays').indexOf(td)<0){
        condition = 'holiday_tomorrow';
    }
    if (global.get('myHolidays').indexOf(tm)>-1 && global.get('myHolidays').indexOf(td)>-1){
        condition = 'holiday_today';
    }
    if (global.get('myHolidays').indexOf(tm)<0 && global.get('myHolidays').indexOf(td)>-1){
        if(hday === 'h_ends'){
        default:
        hday = 'Unknown';

Julian Knight

未读,
2016年6月30日 08:30:302016/6/30
收件人 Node-RED
Sorry, I know I'm just being picky now because I can't spot any actual errors - but I'd sort my case indentation so it is easier to read & therefore spot any errors (though I can't see one) and I would set all the msg variables once and only change the ones I needed to in the switch statement. That would make the code shorter at least.

But I can't spot any errors as I say. Weird.

Dave C-J

未读,
2016年6月30日 09:45:582016/6/30
收件人 node...@googlegroups.com
or indeed reuse the same msg on several outputs...
        msg1 = { payload:"auto" };
        msg2 = { payload:"auto" };
        msg3 = { payload:"auto" };
        msg4 = { payload:"auto" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
...
        node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);

could be

        msg1 = { payload:"auto" };
        msg2 = { payload:"disabled" };
...
        node.send([msg1,msg1,msg1,msg1,msg2,msg2,msg2,msg2,msg2]);

Dave C-J

未读,
2016年6月30日 09:58:282016/6/30
收件人 node...@googlegroups.com
the too many errors level is set to 50 by default... so fairly low... but even so none of us can yet see what 50 errors they may be... 
(search on maxerr) - but of course we use the packed up minified version so a pain to poke into runtime.

steve rickus

未读,
2016年6月30日 10:13:362016/6/30
收件人 Node-RED
I, too, have seen some quirky messages pop up while editing large functions. It seems to happen most often while trying to add comments text inside existing code logic, especially if I pause while typing. Seems it thinks I'm starting some arithmetic expression, and once I finish typing it stays stuck in some non-comment mode... as Julian says, weird.

$0.02 -- I know it sounds strange, but sometimes I can straighten out the editor by cutting all the source text, waiting a second for the editor to adjust to the empty source, and pasting it back in.

Walter Kraembring

未读,
2016年6月30日 13:06:162016/6/30
收件人 Node-RED
or indeed reuse the same msg on several outputs...
        msg1 = { payload:"auto" };
        msg2 = { payload:"auto" };
        msg3 = { payload:"auto" };
        msg4 = { payload:"auto" };
        msg5 = { payload:"disabled" };
        msg6 = { payload:"disabled" };
        msg7 = { payload:"disabled" };
        msg8 = { payload:"disabled" };
        msg9 = { payload:"disabled" };
...
        node.send([msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9]);

could be

        msg1 = { payload:"auto" };
        msg2 = { payload:"disabled" };
...
        node.send([msg1,msg1,msg1,msg1,msg2,msg2,msg2,msg2,msg2]);

This must anyway be the wrong approach to overcome problems !?
The code as is, is actually working, it has been executed many times by now but a warning like that is of course worrying. The lint tool test does not raise any warning of this kind. Getting a message "too many errors" makes you wonder what on earth I have done and creates an uncertainty.

I understand the difficulties and I am grateful to all support given. Could it be that the editor inside the function node has a limitation in number of lines or variables it can handle and present visually?

Kind regards, Walter

Nicholas O'Leary

未读,
2016年6月30日 13:09:372016/6/30
收件人 Node-RED

I don't have a definitive answer for you. I can acknowledge the error is odd and I can't explain it. We will need to investigate it properly by understanding what ace is doing.

My suggestion of using that comment was not the final fix. It was just a workaround to suppress that particular error message to help you carry on.

Nick


--

Walter Kraembring

未读,
2016年6月30日 13:20:422016/6/30
收件人 Node-RED
I have tested further a bit. I think I have a clue; pasting the code into a new function node I started to delete code lines step-by-step and I could see that the error message changed, the scanning was able to work further (originally it stops at 55%). Finally the error went away!!! In total I can have 118 lines of code. Adding one more line, causes the warning to be raised

Julian Knight

未读,
2016年6月30日 18:49:112016/6/30
收件人 Node-RED
OK, that is odd, I'm sure I've had much longer function nodes. Did the editor get updated for the last release of NR?

I'm not at home at the moment so I can't check my live code.

Julian Knight

未读,
2016年6月30日 18:56:092016/6/30
收件人 Node-RED
Ho ho, I've just pasted the code into a function node in my Windows install which is a standard v0.14.3, Node.js v4.4.3 and W10 using Chrome.

And I get the same error at line 128

It is a test instance so I have very little installed. And NR is installed globally as per the standard install instructions.

Nicholas O'Leary

未读,
2016年6月30日 19:10:562016/6/30
收件人 Node-RED

How you have node-red installed and what version of nodejs you are using has no bearing here. It is some configuration issue with the ACE editor widget we use, and that is entirely self-contained within the browser.

We have enough information to go on, and we're at the point where someone needs to dig into the code and debug it properly. The basic questions are:
- why is it hitting so many errors but not flagging anything until you hit a limit (50), and
- what are those errors, given none of us can see anything wrong with the code provided.

Walter - if you wanted to raise an issue on GitHub with the details you've provided here, we can tackle it in due course.

Nicj


--

Julian Knight

未读,
2016年6月30日 19:12:392016/6/30
收件人 Node-RED
Ah, possible lightbulb moment!

If you put 'use strict'; at the start of the code, you get a lot of errors! I suspect this is a bug in jshint. Without use strict it shouldn't be finding errors like it thinking that context is not defined.

Using use strict also found a bug. myMsg is not defined with a var anywhere

I added the following to the top of the code:

/*globals context,msg,node */

'use strict';
var myMsg;

...

There were no errors remaining.

Nicholas O'Leary

未读,
2016年6月30日 19:15:012016/6/30
收件人 Node-RED

Yup, that was going to be the first thing I looked at when I came to debug it properly... we don't tell ace about the globals available to you in a Function. But it is still odd that it doesn't highlight them at all.

Nick


--
回复全部
回复作者
转发
0 个新帖子