Can't execute connection.connect() on node.js

Supporting Browser-based Javascript and Node.js
Post Reply
osrenterprises
Fresh meat
Posts: 2
Joined: Sun Sep 12, 2021 2:11 am
Contact:

Can't execute connection.connect() on node.js

Post by osrenterprises »

I'm trying to develop node.js webserver that interact with my phidgetAnalog 4-Output.
I've installed all the needed libraries according to the Phidget Getting started user guide.

I Create a conn object and try to run conn.connect() but the code keep on crashing to the point it doesn't even breaks in the catch(ex) block of my node.js code.

JS Library (phidget22) version: 1.7 (seems to be the latest)

I tried to go into the phidget22 npm package code and I trace the exception to the following line:
"...}, Vt.prototype.connect = function () {
if (this.connected === !0) return Promise.resolve(this);
--------------------------------------------------------
var t = this; // The code crashes on this line //
--------------------------------------------------------
return new Promise(function (e, n) {

..."

after running the above line of code, it crashes and reach the following catch block:
"...
} catch (s) {
n(new _t(V.UNEXPECTED, s.message));
}
});

..."

What am I doing wrong???

p.s.
I also wrote code in python that intercat with the same device and it works perfectly ON THE SAME development laptop.

my node.js code:
const initPhidgetChannel = () => {

try{
var libraryVersion = phidget22.Phidget.getLibraryVersion();

let conn = new phidget22.Connection(5661, 'localhost');
conn.connect().then(function() {
channel = new phidget22.VoltageOutput();
channel.open(2500).then(function() {
let enabled = channel.getEnabled();
console.log(`InitPhidgetChannel: Enabled: ${enabled}`);
return true;
}).catch(function (err) {
console.error(`Error during channel openning: ${err}`);
return false;
});
}).catch(function(err) {
console.error(`Error during connection: ${err}`);
return false;
});
}
catch(ex)
{
console.error(`PhidgetException: ${ex}`);
return false;
}
}
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Can't execute connection.connect() on node.js

Post by mparadis »

Your code works if I add

Code: Select all

var phidget22 = require("phidget22");
to the top of your program and get rid of the

Code: Select all

const initPhidgetChannel = () => {
and the corresponding close bracket.
osrenterprises
Fresh meat
Posts: 2
Joined: Sun Sep 12, 2021 2:11 am
Contact:

Re: Can't execute connection.connect() on node.js

Post by osrenterprises »

Hi,

Thanks for your reply.
What version of npm package 'phidget22' did you install?

regarding your comments:
1. I have the require statement in the top of my code (forgot to mentioned it in my post).
2. as for the 'const initPhidgetChannel = () => {' - I do the entire connection/channel init in this function which i call it from other js file - so I don't see how running it not through a function call will resolve my problem.

I think my problem is in the version of npm package 'phidget22' which I install (I Currently install the latest one 2.7.4) and maybe the installation of the libphidget22 I did on my Linux machine doesn't work with it ('apt-get install libphidget22').

let me summarzie what I did on my Linux Ubuntu 18.4 machine according to the Phidget 22 manual:
1. apt-get install libphidget22
2. apt-get install libphidget22extra
3. apt-get install phidget22networkserver (but i don't think I need it as all running on the same machine and the phidget device is connected to it)
4. apt-get install phidget22wwwjs
5. npm install phidget22
6. wrote my node.js code
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests