I have a problem with the code of one of my plugins. I would like to make asynchronous calls and use callbacks but it doesn’t seem to work. My callbacks are never executed.
The code below is working on a node v14 but not on the plugin. I can’t debug it very well but when i had an alert in the then arrown function it’s never executed (i don’t have the error alert is not defined).
For example:
indent preformatted text by 4 spaces
const sftpClient = require(‘ssh2-sftp-client’);
const { exit } = require(‘process’);
const https = require(‘https’);
let remote = ‘FOLDER/youhou.json’;
let data = ‘YOURURL’;
Because i can’t see the file on the FTP even if the code is working locally.
Why do you mean by context.async? this? Asynchronous context tracking | Node.js v19.2.0 Documentation
I have also tried this But it’s also only working locally not on Bubble
const sftpClient = require(‘ssh2-sftp-client’);
const https = require(‘https’);
const sftp = new sftpClient();
let data = FILEURL;
let remote = YOURFOLDER;
const toto = new Promise(function (resolve, reject) {
https.get(data, async (stream) => {
try {
await sftp.connect({
host: HOST,
port: PORT,
username: USERNAME,
password: PASSWORD
});
console.log(‘Successfully connected to sftp’);
} catch (error) {
console.log(error);
}
try {
await sftp.put(stream, remote);
resolve(‘done ’);
} catch (err) {
reject(err);
console.log(‘failed:’, err);
}
});
});
adding on to this, you should download your IDE of choice (I recomend WebStorm or, for a more simple solution, VS Code). Then, install Node 14 on your computer (this means that your version of node and Bubble’s will be pretty close to the same, so you know that what works on your computer should work on the sever. Debug everything in your IDE, then copy and paste to the server.
I’ve noticed that .then(…) and also async/await work perfectly on the front-end/client-side, but fail miserably on the backend–unless you do everything inside context.async.
let returnThisToTheUser = context.async(async callback => {
await operation1;
const t = await operation2;
const abc = operation3.then( resp => {
callback(null, resp);
})
if(err) {
callback(err, null);
}
})
return {
"my return var in bubble plugin editor": returnThisToTheUser
}
Note that you do not define what callback is, Bubble does. It is a function that takes parameters (in this order) error and return value. If an error occurs, Bubble is supposed to log it. If not, it assigns the return value to whatever variable you’re using (in my example, this would be returnThisToTheUser (essentially, after the whole async operation occurs, we do let returnThisToTheUser = the second parameter of callback).