C
C#11mo ago
Ketamine Kyle

Updating Html Table when data is added to mongo db

I am making a asp.net core mvc application and I wanna add data to my table every time a entry is added to my mongodb? I have tried using SignalR but it wont work? any ideas on how I could do it
13 Replies
<>
<>11mo ago
yeah use java
Angius
Angius11mo ago
Sure, SignalR will work for real-time communication
Ketamine Kyle
Ketamine KyleOP11mo ago
Im trying to do that atm but I keep getting errors let me send over my code
Nox
Nox11mo ago
!ban 574584522541367306 Here to troll, ✌️
Ketamine Kyle
Ketamine KyleOP11mo ago
public class TableHub : Microsoft.AspNetCore.SignalR.Hub
{
public async Task SendDataToClients(object newData)
{
await Clients.All.SendAsync("UpdateTable", newData);
}
}
public class TableHub : Microsoft.AspNetCore.SignalR.Hub
{
public async Task SendDataToClients(object newData)
{
await Clients.All.SendAsync("UpdateTable", newData);
}
}
var hubContext = this.HttpContext.RequestServices.GetRequiredService<IHubContext<Noble.Hub.TableHub>>();
hubContext.Clients.All.SendAsync("UpdateTable", data);
var hubContext = this.HttpContext.RequestServices.GetRequiredService<IHubContext<Noble.Hub.TableHub>>();
hubContext.Clients.All.SendAsync("UpdateTable", data);
var connection = new signalR.HubConnectionBuilder().withUrl("/myHub").build();

connection.on("UpdateTable", function (newData) {
updateTable(newData);
});

connection.start().catch(function (err) {
return console.error(err.toString());
});

function updateTable(newData) {
var newRow = '<tr>' +
'<td>' + Username + '</td>' +
'<td>' + ID + '</td>' +
'<td>' + newData.Country + '</td>' +
'<td>' + Company + '</td>' +
'<td><button class="btn btn-primary">Download</button></td>' +
'</tr>';

$('#dataTableExample tbody').append(newRow);
}
var connection = new signalR.HubConnectionBuilder().withUrl("/myHub").build();

connection.on("UpdateTable", function (newData) {
updateTable(newData);
});

connection.start().catch(function (err) {
return console.error(err.toString());
});

function updateTable(newData) {
var newRow = '<tr>' +
'<td>' + Username + '</td>' +
'<td>' + ID + '</td>' +
'<td>' + newData.Country + '</td>' +
'<td>' + Company + '</td>' +
'<td><button class="btn btn-primary">Download</button></td>' +
'</tr>';

$('#dataTableExample tbody').append(newRow);
}
Anything im doing wrong?
Angius
Angius11mo ago
You mentioned errors. What would they be?
Ketamine Kyle
Ketamine KyleOP11mo ago
let me re run it and send hold on
Angius
Angius11mo ago
Also, why object?
Ketamine Kyle
Ketamine KyleOP11mo ago
What would be better? System.NullReferenceException: 'Object reference not set to an instance of an object.' on this line var hubContext = this.HttpContext.RequestServices.GetRequiredService<IHubContext<Noble.Hub.TableHub>>();
Angius
Angius11mo ago
The... actual type would be better Whatever type represents your data And the error tells you something here is null So ig try to figure out what that is, with the help of the debugger Also, why GetRequiredServices instead of using the DI container and injecting it? Also also, async method calls have to be awaited
Ketamine Kyle
Ketamine KyleOP11mo ago
The problem is its not null since im literally using the data object in other parts in the same function
Angius
Angius11mo ago
Something is null Could be HttpContext Could be the result of GetRequiredServices() Could be RequestServices Something
Ketamine Kyle
Ketamine KyleOP11mo ago
alright Ill take a look Yeah this was the problem Thanks mate
Want results from more Discord servers?
Add your server