breeze firstordefault query throws error - breeze

I have a web API which returns the firstordefault of a particular data model. If it returns a value then breeze is fine if it returns null breeze throws an error. The breeze query construct executeQuery(query).then(success).fail(failure) goes to the failure function indicating a server failure.The error is thrown on line 13627 of breeze.debug v 1.3.6 while trying to evaluate data.Results. Is this intentional or am I doing something wrong ?

Related

Cypher - query missing parameter throws an exception

I have simple query with $login parameter
MATCH (user: User {login: $login})
RETURN user
when it is set to null in Neo4j Browser with
:param login => null
execution does not throw an exception and returns nothing which is expected.
However when I do not provide $login parameter following is thrown
Neo.ClientError.Statement.ParameterMissing
Expected parameter(s): login
Is there a way to proceed with query even if parameter key is not provided?
P.S.
I think the same issue is adressed here and according to the originator droping WHERE clause should make it work.
If a parameter isn't provided, you will always encounter an error when attempting to use that parameter.
However, you can get around this a bit with a little indirection. Consider passing a map structure as the parameter, which may contain entries for all of your other parameters, optional or not. As long as you're passing in the map parameter, your query should execute, and you can use either dot notation or index/key access into the map to use the parameters from the map.
For example:
:params {params:{name:'Keanu Reeves'}}
We now have a $params parameter in scope, which is a map. Usage can be $params.login or $params['login'], both of which return null for the non-existing entry in the map.

How to find the distinct error messages of model state validation failure in asp.net mvc

I want to filter the Error Messages that gets populated as part of data annotation modelstate validation failure. As in if an array of objects comes as a part of class, and the validation fails for more than one object, I do not want the same message to be added again and again. Instead, I want to find the distinct error messages
string ValidationFailure= string.Join(";", actionContext.ModelState.Values.Distinct().Select(x.ErrorMessage));
But not able to get the required output.
It looks like your attempt is close, but you’re using Distinct on something that’s already unique (Values). Instead, try the following variation:
string ValidationFailure = string.Join(";", actionContext.ModelState.Values.Select(x => x.ErrorMessage).Distinct());
This ensures that you get a distinct list of ErrorMessages.

How to execute local queries with breeze EntityManager in Edmunds sample?

I'm a newby to breeze and wonder if it is possible to run local queries against entities that have been fetched using a REST service like in Edmunds sample.
Is it possible to execute local queries using breeze EntityManager after reading the entities from a remote REST service?
I tried to extend Edmunds sample app with a local query that will be called after all Makes have been loaded:
var query = breeze.EntityQuery
.from("Make:#Edmunds")
.where("niceName", "startsWith", "A")
return manager.executeQueryLocally(query).then(returnResults);
When I execute the query I get the following Exception:
"Cannot find an entityType for resourceName: 'Make:#Edmunds'.
Consider adding an 'EntityQuery.toType' call to your query or calling
the MetadataStore.setEntityTypeForResourceName method to register an
entityType for this resourceName."
What is wrong or missing here? How could I make the local query run?
Breeze is interpretting your query .from() parameter as a resource instead of as a type. (which you appear to be trying to set)
To let Breeze know which type of entity you are trying to query, simply do as it suggest - chain a toType call onto your query -
var query = breeze.EntityQuery .from("Make:#Edmunds").where("niceName", "startsWith", "A").toType('Make')
return manager.executeQueryLocally(query).then(returnResults);

BreezeJS malformed OData query Url when using "startsWith"

Hello I am trying to execute a query using breezejs 1.3.4 . My query is the following
function getContacts() {
var query = breeze.EntityQuery
.from("Contacts").where("Desc", "startsWith", "P");
return manager.executeQuery(query)
.then(getSucceeded).fail(getFailed);
}
"Desc" is a string property in my "Contacts" C# backend model. Tha problem is that the Query URL is formatted like this .../api/Application/Contacts?$filter=startswith(Desc%2Ctime'P')%20eq%20true
The word time is added before "P" and I get a this exception in the Response
{"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"The query specified in the URI is not valid.","ExceptionMessage":"Unrecognized 'Edm.Time' literal 'time'P''
If in the comparison I use a lower case "p" then the Url is costructed as it should be like this
"$filter=startswith(Desc%2C'p')%20eq%20true` .
I don't have the same problem when using other UpperCase letters of the english alphabet.
Does anyone have an idea what am I missing, I can't figure out why the word "time" is added in that specific query?
Thank you.
We were able to reproduce the problem.
While the exception message is confusing, I believe you might be getting the error because you have not associated that the resource name 'Contacts' with an EntityType of presumably 'Contact'.
What is happening is that when Breeze tries to build the query it will usually use its metadata to correctly format the url query string to send to the server. The critical part of this process involves determining the EntityType associated with the resourceName given in the 'from' clause of the query ('Contacts' in your case). Breeze uses a resource name to entity type map to do this, but if it cannot find a mapping, it will still continue to build the url because we still need to support ad-hoc requests against endpoints for which we have no metadata.
You can update this map yourself via the MetadataStore.setEntityTypeForResourceName method. If you are using the Entity Framework, this map is initially populated based on the EntityType name/Entity Set name mapping that is part of your EDMX model. So in your case you can either modify your EDMX model or call the setEntityTypeForResourceName method directly.
Unfortunately, without the metadata Breeze has to infer the datatypes in the query. So in your case
"Desc", "startsWith", "P"
since Breeze can't determine that "Desc" is a 'string' property of your Contract EntityType it tries to infer the 'dataType' of "Desc" based on the value 'P'. Unfortunately, 'P' is a valid ISO8601 'duration' value ( a way to express a 'Time' datatype), so Breeze incorrectly tries to construct a query string with 'P' treated as a 'Time' constant. This is why your code works with a lower case 'p' ( not a valid duration value).
This inference logic can certainly be improved and we have a fix that will allow us to do exactly that coming out in the next release. However... the better and more general solution to this type of issue is to get the 'resourceName/entityType' mappings correct in the first place.
Hope this helps.

Breeze Metadata appears in xml format

Breeze cannot create entities. It does the query for Metadata (which is in my Controller class) and it returns data with a 200 code. Then it queries the data, and returns with a 200 code. Both sets of return data appear to be json format on the surface.
Looking further into the Metadata, it appears to be json serializing the xml metadata. This does not feel right to me - and on the live example on the breeze site, does not appear to be doing this.
Anyone have any tips on what might be causing this? Here is how the metadata starts:
"{\"?xml\":{\"version\":\"1.0\",\"encoding\":\"utf-8\"},\"schema\":{\"namespace\":
When the service returns data, I get an error, with the full & correct json response. The internalError reports "Unable to get property 'createCtor' of undefined or null reference"
It gets to this line in the mergeEntity function, when I debug breeze.js:
targetEntity = entityType._createEntityCore();
Then in the ctr.prototype.getEntityCtor function - it fails to get the entity's constructor from the metadataStore's _typeRegistry property using this line:
var aCtor = typeRegistry[this.name] || typeRegistry[this.shortName];
after that, the error is thrown and we end up in the catch of the executeQuery function.
The issue I was having should have been obvious. My bundling configuration was not including knockout scripts after the scripts have been updated. Be sure that you have a binding library loaded (and it's loading properly) if you experience this issue.

Resources