user1206480
user1206480

Reputation: 1858

Uncaught Type Error

I am working on my first SignalR code, and I am running into an uncaught type error. My code initially works, as my hub controller returns a serialized array back to the client. The problem occurs when I try to iterate through the array using the $.each() method. The exact error I receive is this:

Uncaught TypeError: Cannot use 'in' operator to search for '95' in [{"OptionsId":3,"MembershipLevel":"Gold","MembershipLevelId":2,"Days":0,"Months":1,"Fee":20.00}]

I don't know what this means, so I am not sure if this is SignalR related, jQuery, or something else. Here is my code.

jQuery:

var getOptions = function () {
// reference the auto generated proxy for the hub
var vR = $.connection.vendorRegistration;

// create the function that the hub can call back to update option
vR.client.updateMembershipOptions = function (returnOptions) {
    // update the selectList
    $("select[name=SelectedMembershipOption]").empty();

    // this is where I receive the error
    $.each(returnOptions, function (index, memOption) {
        $("select[name=SelectedMembershipOption]").append(
            $("<option/>").attr("value", memOption.OptionsId)
              .text(memOption.MembershipLevel)
            );
    });
};

// Start the connection
$.connection.hub.start().done(function () {
    $("#VendorType").change(function () {
        // call the ReturnMembershipOptions method on the hub
        vR.server.returnMembershipOptions($("#VendorType")
            .val());
    });
});
};

Server Side:

public class VendorRegistration : Hub
{
    public void ReturnMembershipOptions(int vendorTypeId)
    {
        // get the options
        var options = AcoadRepo.Vendors.ApiLogic.MembershipOptions
            .ReturnOptions(vendorTypeId);

        // serialize the options
        var returnOptions = JsonConvert.SerializeObject(options);

        // call the updateMembershipOptions method to update the client
        Clients.All.updateMembershipOptions(returnOptions);
    }
}

Upvotes: 0

Views: 246

Answers (1)

N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

The reason why you're getting the error that you are is because you're trying to traverse a string. The reason why it's trying to traverse a string is because of this line:

// serialize the options
var returnOptions = JsonConvert.SerializeObject(options);

SignalR will serialize your object for you, so since you're serializing in your hub method the data that's going over the wire is double serialized which then represents a plain string, therefore when it hits the client it's deserialized as a string.

Performing a .each on a string will throw because it requires that the calling argument is either an array or an object.

To fix your issue just remove your serialization and just call the function with your options, aka:

public class VendorRegistration : Hub
{
    public void ReturnMembershipOptions(int vendorTypeId)
    {
        // get the options
        var options = AcoadRepo.Vendors.ApiLogic.MembershipOptions
            .ReturnOptions(vendorTypeId);

        // call the updateMembershipOptions method to update the client
        Clients.All.updateMembershipOptions(options);
    }
}

Upvotes: 2

Related Questions