How generate new msg, for use outside node.on('input',cb)

Hi,

I had an output node who listen directory. I want use jsonata on some properties, process like this

            const expr = RED.util.prepareJSONataExpression(node.ignoredFiles,node)
            let msg = {}
            RED.util.evaluateJSONataExpression(expr, msg, rep =>{
              console.log("******",rep,node);
              return rep
            })

evaluateJSONataExpression need a msg... I don't understand why msg is needed, and i haven't.

The code above won't works... somebody can explai me how to use evaluateJSONataExpression without msg or if they are a function on node object for create msg ?

Thanks

There are two parts here - prepareJSONataExpression and then evaluateJSONataExpression.

The prepare step takes two arguments:

  • the JSONata expression (as a string)
  • the node evaluating the expression. This is needed so if the $flowContext/$globalContext functions are used, it knows what node to use to access context.

The evaluate step is where the expression is actually run. It takes two arguments:

  • the prepared expression (as returned by prepareJSONataExpression)
  • the object to evaluate the expression against - usually a message, but it can be whatever object you want the expression to evaluate against.

In your case, what sort of expression does node.ignoredFiles contain? It's hard to say what you should use as the second arg to evaluate without knowing what sort of expression you want the user to provide.

ok, so msg would be the filename but syntaxe ?

let msg = { filename : filename }
 RED.util.evaluateJSONataExpression(expr, msg, rep =>{
              console.log("******",rep,node);
              return rep
            })

evaluateJSONataExpression check every properties of msg ?

My Jsonata expr

`$not($contains(file, /(.*)\.(jpe?g)$/))`

The goal it's to exclude filename on pattern. The input accept regex of course, but since my input need return true or false (i.e. don't treat this new file cause name doesn't satisfy the jsonata expr or the regex) i want use jsonata too

Thanks

let msg = {file}
const expr = RED.util.prepareJSONataExpression(node.ignoredFiles,node)
console.log(expr.evaluate(msg));

works as excepted (i.e. return true or false)

but

let msg = {file}
const expr = RED.util.prepareJSONataExpression(node.ignoredFiles,node)
RED.util.evaluateJSONataExpression(expr,msg, function(rep) {
  console.log("******",rep);
  return rep
})

return null

hum it's embarrassing... newbie fault, by convention first argument of callback it's err...

By the way, thanks for your time and for your explanation.

Really thankfull.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.