Async functions in function node [ webdav ]

Hi ,
I'm trying to use the [WebDav] library (GitHub - perry-mitchell/webdav-client: WebDAV client written in Typescript for NodeJS and the browser) and I'm having trouble returning the error (either credentials or url)

The thing is that the example functions, in addition to being asynchronous, are of the type:
const directoryItems = await client.getDirectoryContents ("/");

I want to know if you have experience with asynchronous functions in this environment and how you do this.
I add a couple of examples of how I do this, but ask why I can't return an error

(the msg.client is the client preconfigured on another node)
Example 1 :

const start = async function(a) {
try{
msg.payload = await msg.client.exists(a);
if(msg.payload===true){
node.send([msg,null])
}
else{
node.status({ fill: "yellow", shape: "ring", text: "Does not exists" })
node.send([null,msg])
}
}
catch(err){
error = err
// node.send([null,msg])
// node.error("la dirección no existe")
// throw new Error ( err )
}
}
// Call start
start("/myNextCloudDirectory");

Example 2 :

msg.client.exists("/myNextCloudDirectory")
.then(function(exists) {
msg.exists = exists
if(exists===true){
node.send([msg,null])
}
else{
node.status({ fill: "yellow", shape: "ring", text: "Does not exists" })
node.warn("No existe el directorio")
node.send([null,msg])
}
}).catch(function(e) {
error = err
// node.send([null,msg])
// node.error("la dirección no existe")
// throw new Error ( err )
});

On both catch i cant send or throw anything only node.warn, log or error

(Thanks in advance)

wow, if in the catch I make a concat, it does the node.send, why is this?

.catch(function(e) {
msg.error= "".concat(e)
//msg.error= e
// msg.e = {e:e}
node.send([null,msg])
node.warn(e)
});

Please wrap your code in code tags so that it is readable.

What is creating the payload.client.exists() function? And why do you assume that you can pass a function in a msg?

Probably for the same reason the await doesn't work - the exists() function doesn't exist so you get an error and that error is correctly caught?

You should have shared the error details as well.


I'm afraid your code doesn't make much sense. There is really no point in making a promise dependent on an incoming msg since an incoming msg is already asynchronous.

The only thing that makes sense would be to do the whole transaction in a single function node. In other words, create the instance of the library, connect to the server, get what you want. That returns a promise that you can await and do a node.send() on resolution.

You could, if you wanted to, move the instance creation to settings.js so only pass the instance instead of the require().

yes, sorry for the code, I cut out how I have it done and it seems to make no sense

the point is that I want to know how to use this library in a function node.
also know how to do that if the credentials or the server are incorrect, an error can be sent.

the webdav examples are asynchronous functions and when you add them as is in a function node it sends an error : "SyntaxError: await is only valid in async function"

in the ways I indicated at the beginning, I can get it to work fine: receive files, know if a directory exists...

my problem is error handling

Edit: when i send msg.error, but
instead of

msg.error = "" .concat (e)

I make

msg.error = e.message,

I get the error too.
I do not understand this at all

Ah, I already deleted my test. I took the example code from the MDN pages on async/await can converted the console.logs to msg.sends. That worked just fine. So there is certainly not an issue with using async/await in a node-red function node.

But as I said previously, this only makes sense if the whole of the logic is in the 1 function node. So add a reference to the library in settings.js so that it is added to the global vars. Then get it in the function node. Then you should be able to use their example code exactly as-is. Once that is working, just make the variables you need to change such that they are picked up from the incoming msg & feed them into the function node.

That doesn't make sense. e is an object, you can't concat it to an empty string, that looks like Python not JavaScript.

1 Like

ok, the ".then(). catch()" method works, but the error is not reported if you send "e". For some reason this error is not reported.
You are right that "e" is an object, since doing the following:

.catch(function(e) {
    msg.error=JSON.stringify(e)
     node.send([null,msg])
    node.warn(e)
});

the content of "e" is sent,
output:

image

but if you look at node.warn this is read correctly, that's what confused me
and the concat also confused me too

and finally if i do a msg.error=JSON.parse(JSON.stringify(e)), it sends de object correctly...

I think that is a noe.js or javascript thing. I've noticed it on other occasions not just in Node-RED (I think anyway). I think that something suppresses the output.

What you wanted is to output e.message which should always be present on an error object.

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