Azure/azure-event-hubs-node

tx.send is not a function

Closed this issue · 7 comments

From @ArunAdPushup on April 4, 2017 10:43

Hi,

While trying to send data to eventhubs using azure-event-hubs package v0.0.7, I am getting the following error -

screen shot 2017-04-04 at 4 00 42 pm

Here's the code -

var EventHubClient = require('azure-event-hubs').Client;
 
var client = EventHubClient.fromConnectionString('Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 'myeventhub')
client.open()
    .bind(client.createSender.bind(client))
    .then(function (tx) {
        tx.on('errorReceived', function (err) { console.log(err); });
        tx.send({ contents: 'Here is some text sent to partition key my-pk.' }, 'my-pk'); 
    });

I am following Example 3 to create a sender, as shown here.

Copied from original issue: Azure/azure-event-hubs#312

I think there's a typo in the code you pasted here, after client.open(), you're calling bind instead of then. can you check and see if this is also the case in the code you ran, and if that case let us know if you still encounter the issue after you replace it with then?
It should look like this:

[...]
client.open()
    .then(client.createSender.bind(client))  // <-- that's the corrected line
    .then(function (tx) {
        tx.on('errorReceived', function (err) { console.log(err); });
        tx.send({ contents: 'Here is some text sent to partition key my-pk.' }, 'my-pk'); 
    });

I tried your code snippet, now I am receiving this error -

screen shot 2017-04-05 at 1 30 56 am

My bad, I had corrected the bind/then thing but not looked around it.

ArgumentOutOfRangeError errors usually happen when the partition id used (if any) is not valid.

I think what might be happening is that the result of the open() promise is used as an argument for the createSender() promise and that won't work. See the call to createSender() into its own function in our sample:

https://github.com/Azure/azure-event-hubs-node/blob/master/samples/javascript-eventhub-sample/simple_eventhubs_client.js#L44

can you try and run this basic sample (in the link above) just replacing the connection string and see if this works? I'm going to look if we have a problem with the Readme sample.

It seems to be working fine now, thanks a lot.

I have one question though, how does this exactly work ? We did not specify a partition id here, in our call to createSender(). Which partition is selected for getting the event data and how ?

Specifically in this case, what happens is that the open() promise returns the Client instance, and that Client instance is passed to the createSender() promise, which tries to interpret it as a partition id, and fails because it looks like '[object Object]' which is obviously not valid. The README is wrong, I'm going to change it.

Generally speaking, when no partition is specified, a partition is automatically assigned by the Event Hubs instance. if a partition key is specified, then that partition key is hashed and used to make sure all messages with the same partition key end up in the same partition.

Thanks a lot for reporting this issue, glad it's resolved, and sorry you ran into it!

Fixed. 3be0b72

Not a problem at all! Thank you for the explanation.