0
votes

Getting an error client side with breeze: "Cannot call method 'map' of undefined" when trying to pull over some data. The difference between this action and one that works is that this action is calling a stored procedure and returning ObjectResult<T> instead of DbSet<T>.

Might this be why I get an error? Using Chrome Developer tools, I do see that the breeze controller is returning json data.

I have created a complex model type in the edmx for mapping the rows returned from the stored procedure.

The action in the breeze controller has a return type of IEnumerable<T>.

3

3 Answers

1
votes

I experienced the same error when using an EF complex type. A workaround was to create a view in my database instead of using a complex type, set the stored procedure to return a type of the new view which had a primary key and then it worked. It would seem that breeze requires entities to have a primary key defined.

0
votes

Hm... not quite sure what is happening, so just guessing here, but try adding an AsQueryable() to the result returned, and changing the result type to a IQueryable.

We don't have any stored proc tests for breeze yet, but this is impetus for me to add some :)

0
votes

I had the very same issue, but thank God I figured out a solution. Instead of using a stored procedure, you should use a view, as Breeze recognizes views as DbSet<T>, just like tables. Say you have a SQL server table that contains two tables Customers and Orders.

Customers (**CustomerId**, FirstName, LastName)
Orders (OrderId, #CustomerId, OrderDate, OrderTotal)

Now, say you want a query that returns orders by CustomerId. Usually, you would do that in a stored procedure, but as I said, you need to use a view instead. So the query will look like this in the view.

Select o.OrderId, c.CustomerId, o.OrderDate, o.OrderTotal
from dbo.Orders o inner join dbo.Customers c on c.CustomerId = o.CustomerId

Notice there is no filtering (where ...). So:

i. Create a [general] view that includes the filtering key(s) and name it, say, OrdersByCustomers

ii. Add the OrdersByCustomers view to the entity model in your VS project

iii. Add the entity to the Breeze controller, as such:

public IQueryable<OrdersByCustomers> OrdersByCustomerId(int id)  
{
    return _contextProvider.Context.OrdersByCustomers
                                   .Where(r => r.CustomerId == id);
}

Notice the .Where(r => r.CustomerId == id) filter. We could do it in the data service file, but because we want the user to see only his personal data, we need to filter from the server so it only returns his data.

iv. Now, that the entity is set in the controller, you may invoke it in the data service file, as such:

var getOrdersByCustomerId = function(orderObservable, id)
{
    var query = breeze.EntityQuery.from('OrdersByCustomerId')
                                  .WithParameters({ CustomerId: id });

    return manager.executeQuery(query)
                  .then(function(data) {
                      if (orderObservable) orderObservable(data.results);
                  }
                  .fail(function(e) {
                      logError('Retrieve Data Failed');
                  }
}

v. You probably know what to do next from here.

Hope it helps.