Search code examples
signalrsignalr-hubsignalr.client

SignalR, Not a Function


I am trying to send some data to my SQL server. With a model at hand. I got an error saying that my AddBug is not a function. Post:29 Uncaught TypeError: chat.client.AddBug is not a function So can someone please help me in debugging this error. I cannot find any mistake and sorry I am totally new to signalR and trying to get somethings up and running for my server.

Client-Side

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<meta name="viewport" content="width=device-width" />
<title>Post</title>
<script>
    var chat;
    $(document).ready(function () {
        chat = $.connection.addItems;
        console.log('Connection done');
        $.connection.hub.start();
        console.log('Connection secured');
    });
    function test() {
        console.log('Clicked');
        var a = {
            Id: $('#txtId').val(),
            Name: $('#txtName').val()
        };
        console.log('finish a');
        chat.client.AddBug(JSON.stringify(a));
        console.log('error');
    };
</script>

My Hub Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using WebAPItest2.Models;
using System.Data.Entity;
using Microsoft.AspNet.SignalR.Hubs;

namespace WebAPItest2
{
    [HubName("addItems")]
public class queryHub : Hub
{

    public void AddBug(Test bug)
    {
        try
        {
            using (var db = new testDBEntities())
            {
                var b = db.Tests.Create();
                b.Id = bug.Id;
                b.Name = bug.Name;

                db.Tests.Add(b);
                db.SaveChanges();
                int id = b.Id;

                Clients.Others.AddBug(b);
                Clients.Caller.AddMeBug(b);
            }
        }
        catch (Exception ex)
        {
            Clients.Caller.reportError("Unable to create bug.");
        }
    }
}
}

Controller

        public ActionResult Post()
    {
        return View();
    }

Solution

  • //hub code
    
    public void AddBug(string id, string name) 
    { 
       //your code
    }
    
    //js server call
    
    window.chat.server.addBug($('#txtId').val(), $('#txtName').val());