Streampunk / grandiose

Node.JS native bindings to Newtek NDI(tm).
Apache License 2.0
184 stars 47 forks source link

how to fetch the result into a var ? #10

Closed fgraux closed 4 years ago

fgraux commented 4 years ago

Hi there your module seems to be great, but at this time after several days of trying, I'm still unable to put the array result into a var (ndiList) to send it to my html casparcg client. The only thing I succeed is to display it in the console (pretty sad, no ?). Here is my code with a promise to wait for the answer to come before sending it... Would you please help me ?

function ndi_detect(z, callback){
const jeanpierre = require('grandiose'); var ndiV = jeanpierre.version(); const ndi = jeanpierre.find({ showLocalSources: false, extraIPs: [ z ] }); var ndiList;

function getNDI() {
        return new Promise(function(resolve, reject) {
            ndi.then(function(error, response, body) {
                if (error) return reject(error);
                resolve(body);
                });
        });
}

async function main() {
    try {
        let ndiList = await getNDI();
        } catch(error) {
        console.error(error);
        }
        IOSendData(['web'], 'ndilist', ndiList);
        callback(undefined, "\n"+dateTimeNow()+"\t^#^B^W "+ndiV+"\t ^ \tList Loaded: oui!\t ^ \t"+ndiList)
}
main();

}

yatsenkoleg commented 4 years ago

const grandiose = require('grandiose'); let ndiList = await grandiose.find();

fgraux commented 4 years ago

Thanks for your quick reply, I tried this

function ndi_detect(z, callback){
    const grandiose = require('grandiose');
    let ndiList = await grandiose.find();   
    IOSendData(['web'], 'ndilist', ndiList);
    callback(undefined, "\n"+dateTimeNow()+"\t^#^B^W \t ^ \tList Loaded: oui!\t ^ \t"+ndiList)
    }

and it caused the app crashing.

fgraux commented 4 years ago

hello I made up a new install of grandiose on an other computer, set up a simple js file:

const grandiose = require('grandiose');
let ndiList = await grandiose.find();

still same issue : the app crashes. If I suppr 'await', the app remains open but nothing come. Please can you help me ?

fgraux commented 4 years ago

I change my precedent function while adding async:

async function ndi_detect(z, callback){
    const grandiose = require('grandiose');
    let ndiList = await grandiose.find();   
    IOSendData(['web'], 'ndilist', ndiList);
    callback(undefined, "\n"+dateTimeNow()+"\t^#^B^W \t ^ \tList Loaded: oui!\t ^ \t"+ndiList)
    }

It doesnt crash anymore, now I have a return, but instead of the expected [ {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''} ] it return: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

fgraux commented 4 years ago

I change my precedent function while adding async:

async function ndi_detect(z, callback){
    const grandiose = require('grandiose');
    let ndiList = await grandiose.find();   
    IOSendData(['web'], 'ndilist', ndiList);
    callback(undefined, "\n"+dateTimeNow()+"\t^#^B^W \t ^ \tList Loaded: oui!\t ^ \t"+ndiList)
    }

It doesnt crash anymore, now I have a return, but instead of the expected [ {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''}, {name: '', urlAddress: ''} ] it return: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

fgraux commented 4 years ago

Just added a ndiList = JSON.stringify(ndiList ); and everything is now working (SO) well !!! Sorry and thanks