exromany / loopback-counts-mixin Goto Github PK
View Code? Open in Web Editor NEWA mixin to enable get count of related models for a loopback Model.
License: MIT License
A mixin to enable get count of related models for a loopback Model.
License: MIT License
I have the following relation,
file: grade.json
"relations": {
"students": {
"type": "hasMany",
"model": "Account",
"foreignKey": "gradeId",
"through": "Family",
"keyThrough": "studentId"
},
"mixins": {
"Counts": true
}
}
Any request to /grades gives the following response:
{
"error": {
"statusCode": 500,
"name": "AssertionError [ERR_ASSERTION]",
"message": "Invalid relation type: undefined",
"generatedMessage": false,
"code": "ERR_ASSERTION",
"actual": false,
"expected": true,
"operator": "==",
"stack": "AssertionError [ERR_ASSERTION]: Invalid relation type: undefined\n at validateRelation (/api/node_modules/loopback-datasource-juggler/lib/datasource.js:549:3)\n at /api/node_modules/loopback-datasource-juggler/lib/datasource.js:515:7\n at Array.forEach (<anonymous>)\n at DataSource.defineRelations (/api/node_modules/loopback-datasource-juggler/lib/datasource.js:511:28)\n at DataSource.setupDataAccess (/api/node_modules/loopback-datasource-juggler/lib/datasource.js:648:8)\n at DataSource.attach (/api/node_modules/loopback-datasource-juggler/lib/datasource.js:882:8)\n at Function.ModelClass.attachTo (/api/node_modules/loopback-datasource-juggler/lib/model-builder.js:382:16)\n at /api/server/server.js:57:29\n at Layer.handle [as handle_request] (/api/node_modules/express/lib/router/layer.js:95:5)\n at trim_prefix (/api/node_modules/express/lib/router/index.js:317:13)\n at /api/node_modules/express/lib/router/index.js:284:7\n at Function.process_params (/api/node_modules/express/lib/router/index.js:335:12)\n at next (/api/node_modules/express/lib/router/index.js:275:10)\n at nosniff (/api/node_modules/dont-sniff-mimetype/index.js:4:5)\n at Layer.handle [as handle_request] (/api/node_modules/express/lib/router/layer.js:95:5)\n at trim_prefix (/api/node_modules/express/lib/router/index.js:317:13)"
}
}
$scope.data = Institution.courses({
id: $stateParams.institution,
filter: {
include: 'institution',
counts:'batches'
}
});
Here Count is Not Returning..
Hi, I'm currently using loopback3.x with Angular2. The current SDK generater generates codes with filter in header so that I can't use this package properly. Is it possible to add support for filter in headers?
If I try to call GET /resource
- it works clear, but if I try to call GET /resource/{id}
with the mixin, I will get the error:
{
"error": {
"statusCode": 500,
"message": "Internal Server Error"
}
}
I think you should change the behavior of this case.
Hope it is clear for you. If you have questions, don't hesitate, ask me.
Using the example from the readme, the following would return the "game" object corresponding to "id", with the added field of "playersCount":
http://0.0.0.0:3000/api/games/{id}?filter={"counts":"players"}
If a user were to invoke the following (assuming the id of 12345 does not exist in the db), loopback would return a 404.
http://0.0.0.0:3000/api/games/12345
However, when a user does the same call with a counts filter, loopback returns a 500 error.
http://0.0.0.0:3000/api/games/12345?filter={"counts":"players"}
"error": {
"name": "TypeError",
"status": 500,
"message": "Cannot read property 'players' of null",
"stack": "TypeError: Cannot read property 'players' of null
at ...node_modules/loopback-counts-mixin/counts.js:41:24
at Array.map (native)
at ...node_modules/loopback-counts-mixin/counts.js:40:36
at Array.map (native)
at fillCounts (...node_modules/loopback-counts-mixin/counts.js:39:34)
at injectCounts (...node_modules/loopback-counts-mixin/counts.js:16:5)
at Function.<anonymous> (...node_modules/loopback/lib/model.js:216:11)
at execStack (...node_modules/strong-remoting/lib/remote-objects.js:485:26)
at RemoteObjects.execHooks (...node_modules/strong-remoting/lib/remote-objects.js:497:10)
at phaseAfterInvoke (...node_modules/strong-remoting/lib/remote-objects.js:661:10)"
}
Is it possible to add in a sanity check, so that if a 404 is going to be sent back we dont even try the counts? That way we still send back a 404?
I'm using this mixin and wondering the easiest way to filter by the counts.
For example, I have a Post with a number of Ratings. I want to return a subset of Posts with a certain number of ratings, e.g.
Post.find({ where: { ratingsCount: { gt: 200 }}})
But this doesn't work, of course.
Is there a way to achieve this? Even with raw SQL?
It works with REST api, but not works in remote method, how to add it to remote query?
Game.find({
counts:"players"
},cb)
This didn't work
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.