Skip to content

Access NR global context inside function #31

@tmdoit-zz

Description

@tmdoit-zz

I'm trying to use external moment.js lib without luck. Here is my code:

CREATE FUNCTION getWeek AS 
``
    function(x) { 
        let moment = global.get('moment');
        return moment(x).week();
    }
``;
SELECT 
    COUNT(*), 
    SUBSTRING(created_at,1,10) AS created_at,
    getWeek(created_at)
FROM ?
GROUP BY SUBSTRING(created_at,1,10)

I failed also when tried to acceess msg object. Is there a way to resolve it?

EDITED:
As a workaround I used moment in function node before using alasql node so I can work with "ready" data.

let moment = global.get('moment');
for (let i = 0; i < msg.payload.length; i++) {
    msg.payload[i].week = moment(msg.payload[i].created_at).week(),
    msg.payload[i].month = moment(msg.payload[i].created_at).month(),
    msg.payload[i].yaer = moment(msg.payload[i].created_at).year()
}
return msg;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions