Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node object is always undefined #1

Open
masalinas opened this issue Dec 13, 2015 · 3 comments
Open

node object is always undefined #1

masalinas opened this issue Dec 13, 2015 · 3 comments

Comments

@masalinas
Copy link

In the javascript file asociated with your node I have an asyncronous called, so I must use the send fuction of the node object to call the next node, i obtain an error because the node object is undefined always.

This is my code:

Device.find( { where: { deviceHubId: '566d85a22b937b4350d88d19' }, include: 'deviceType' }, function(err, devices) {
    if (err) return err;

    msg.payload = devices;
    node.send(msg);
});

return;

The same code is tested inside a function node and the result is ok, but if use your node with the same code the node object is undefined and crash.

Regards.

@masalinas
Copy link
Author

I resolve the problem patching your code (file-function.js) like this:

function sendResults(node,_msgid,msgs) {
        if (msgs == null) {
            return;
        } else if (!util.isArray(msgs)) {
            msgs = [msgs];
        }
        var msgCount = 0;
        for (var m=0;m<msgs.length;m++) {
            if (msgs[m]) {
                if (util.isArray(msgs[m])) {
                    for (var n=0; n < msgs[m].length; n++) {
                        msgs[m][n]._msgid = _msgid;
                        msgCount++;
                    }
                } else {
                    msgs[m]._msgid = _msgid;
                    msgCount++;
                }
            }
        }
        if (msgCount>0) {
            node.send(msgs);
        }
    }

var sandbox = {
            console:console,
            util:util,
            Buffer:Buffer,
                        node: node,
            __node__: {
                log: function() {
                    node.log.apply(node, arguments);
                },
                error: function() {
                    node.error.apply(node, arguments);
                },
                warn: function() {
                    node.warn.apply(node, arguments);
                },
                send: function(id, msgs) {
                    sendResults(node, id, msgs);
                },
                on: function() {
                    node.on.apply(node, arguments);
                },
                status: function() {
                    node.status.apply(node, arguments);
                }
            },
            context: {
                global:RED.settings.functionGlobalContext || {}
            }
        };

I injected the node objct in the sandbox to have access from the js script file, and configure send node event to called in asyncronous way.

What is your opinion???

@masalinas masalinas changed the title node object is undefined node object is always undefined Dec 13, 2015
@shooftie
Copy link

Same for the context... I don't have access to context.get(), flow.get() etc...

@masalinas , you seem to know your way around better than I, can you point me in the right direction?

[edit]

I patched it by adding context.flow = node.context().flow; just after var context = vm.createContext(sandbox);.

It's a hack until we hear anything...

@hreimer
Copy link

hreimer commented Jan 6, 2017

I had the same problems and your hacks helped me. I also added context.global = node.context().global; just under the context.flow = node.context().flow; line to make global.get and global.set available.

I know this repository has been quite dead but maybe someone has an answer:

The asynchronous node.send() solution above doesn't work the same as in the regular function node when I use an array to output the message to more than one output, e.g. node.send([msg, msg]).
With the normal function, 10 files are being processed and from every file 2 images and 1 pdf are created. However, with the file-function node, only the last file gets processed, but then it is processed 10x.

Can anybody point me in the right direction where I could try to fix that in the code? It must be somewhere in the sendResults(node,_msgid,msgs)function above - it seems the messages are stored in an array and send all at once, but the same function is used in the built-in function node in nodes/core/core/80-function.js and therefore must work correctly I suppose?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants