the (find, findbyid, create) method has not been setup loopback - loopbackjs

I'm trying to setup loopback and so far I've created a db connection to mysql, a model, and am now using the api explorer to test it but I keep getting this error on any call:
"The find method has not been setup loopback. The PersistedModel has not been correctly attached to a DataSource!
Here is the complete response:
{
"error": {
"name": "Error",
"status": 500,
"message": "Cannot call EaAssets.find(). The find method has not been setup. The PersistedModel has not been correctly attached to a DataSource!",
"stack": "Error: Cannot call EaAssets.find(). The find method has not been setup. The PersistedModel has not been correctly attached to a DataSource!\n at throwNotAttached (C:\\cmdb\\node_modules\\loopback\\lib\\persisted-model.js:62:11)\n at Function.find (C:\\cmdb\\node_modules\\loopback\\lib\\persisted-model.js:210:5)\n at SharedMethod.invoke (C:\\cmdb\\node_modules\\strong-remoting\\lib\\shared-method.js:252:25)\n at HttpContext.invoke (C:\\cmdb\\node_modules\\strong-remoting\\lib\\http-context.js:384:12)\n at phaseInvoke (C:\\cmdb\\node_modules\\strong-remoting\\lib\\remote-objects.js:645:9)\n at runHandler (C:\\cmdb\\node_modules\\loopback-phase\\lib\\phase.js:135:5)\n at iterate (C:\\cmdb\\node_modules\\async\\lib\\async.js:146:13)\n at Object.async.eachSeries (C:\\cmdb\\node_modules\\async\\lib\\async.js:162:9)\n at runHandlers (C:\\cmdb\\node_modules\\loopback-phase\\lib\\phase.js:144:13)\n at iterate (C:\\cmdb\\node_modules\\async\\lib\\async.js:146:13)"
}
}
here is my model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"EaApplication": {
"dataSource": "db",
"public": true
},
"EaAssetCapabilityRelation": {
"dataSource": "db",
"public": true
},
"EaAssetTyp": {
"dataSource": "db",
"public": true
},
"EaAssetStatus": {
"dataSource": "db",
"public": true
},
"EaAssetLocation": {
"dataSource": "db",
"public": true
},
"EaAssetRelation": {
"dataSource": "db",
"public": true
},
"EaAssets": {
"dataSource": null,
"public": true,
"$promise": {},
"$resolved": true
},
"EaCapability": {
"dataSource": "db",
"public": true
},
"EaRelationType": {
"dataSource": "db",
"public": true
},
"EaServers": {
"dataSource": "db",
"public": true
},
"EaServices": {
"dataSource": null,
"public": true,
"$promise": {},
"$resolved": true
},
"EaBuildTyp": {
"dataSource": "db",
"public": true
}
}

Your EaAssets model has a datasource of null so the built-in remote methods are not getting attached

Related

loopback.js `POST /foo` isn't offered when using relations

I need a series of endpoints like:
POST /company
POST /company/{id}/customers
POST /company/{id}/customers/{fk}
GET /company/{id}/customers/{fk}
I get #2 and #3 ok, but not number 1 - so, when I try GET /company/{id}/customers/{fk} - it naturally tells me that company id xxx doesn't exist. Yet, the endpoints don't exist.
Below is my configuration if anyone can explain what I am missing
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"customer": {
"dataSource": "memory",
"public": false
},
"company": {
"dataSource": "memory",
"public": true
},
"payment": {
"dataSource": "memory",
"public": false
},
"invoice": {
"dataSource": "memory",
"public": false
},
"integration": {
"dataSource": "memory",
"public": true
}
}
Company model:
{
"name": "company",
"plural": "companies",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"customers": {
"type": "hasMany",
"model": "customer",
"foreignKey": "id"
},
"payments": {
"type": "hasMany",
"model": "payment",
"foreignKey": "id"
},
"invoices": {
"type": "hasMany",
"model": "invoice",
"foreignKey": "id"
},
"integration": {
"type": "hasOne",
"model": "integration",
"foreignKey": "companyId",
"primaryKey": "id"
}
},
"acls": [],
"methods": {}
}
Customers (properties removed for brevity sake):
{
"name": "customer",
"plural": "customers",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"validations": [],
"relations": {
"company": {
"type": "belongsTo",
"model": "company",
"foreignKey": "id",
"primaryKey": "id"
}
},
"acls": [],
"methods": {}
}

Remove pk in the loopback swagger

In loopback, how could I remove the PK from the swagger json displayed.
This is the json displayed in my swagger:
{
"user_id": "string",
"order_type": "string",
"date": "2016-04-28",
"payload": {
"id": 0
},
"id": 0
}
image detail- Swagger
how could I remove the "id": 0 ?
This is my order.json :
{
"name": "Order",
"plural": "Orders",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"user_id": {
"type": "string",
"required": true
},
"order_type": {
"type": "string",
"required": true
},
"date": {
"type": "date",
"required": true
},
"payload": {
"type": "Payload",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
This is my order.js:
module.exports = (Order) => {
Order.create = function(body, cb) {
//
}
Order.remoteMethod('create', {
'http': {'path': '/'},
'accepts': [
{'arg': 'body', 'type': 'Order', 'required': true, 'http': { 'source': 'body' } }
]
});
};
Hidden property did the trick
{
"name": "Order",
"plural": "Orders",
"base": "Model",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
...
},
"validations": [],
"relations": {},
"acls": [],
"methods": {},
"hidden": ["id"]
}

Has many through relation, count always returns 0

I have a table of companies, a table of addresses and a companiesaddresses table to normalize the many to many relationship. Loopback is working just fine to add a company, add an address to a company and I can even query using the API explorer an address associated with a company using the (POST /Companies/{id}/addresses/{fk}) method.
However, when I try to get all the addresses associated with a company (GET /Companies/{id}/addresses) I get back an empty array. Also when I perform a count on how many addresses a particular company has ( GET /Companies/{id}/addresses/count) I always get 0.
I'm sure I'm missing something really tiny. My datasource is postgresql.
/* ----- common/models/companies.json ----- */
{
"name": "Companies",
"base": "User",
"strict": true,
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"companyName": {
"type": "string",
"required": true
},
"firstName": {
"type": "string",
"required": true
},
"lastName": {
"type": "string",
"required": true
},
"cellNumber": {
"type": "string"
}
},
"validations": [],
"relations": {
"addresses": {
"type": "hasMany",
"model": "Addresses",
"foreignKey": "addressesid",
"through": "CompaniesAddresses"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "create"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
}
],
"methods": {}
/* ---- common/models/addresses.json ---- */
{
"name": "Addresses",
"base": "PersistedModel",
"strict": true,
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"streetaddress2": {
"type": "string"
},
"phonenumber2": {
"type": "string"
},
"phonenumber1": {
"type": "string",
"required": true
},
"zippostalcode": {
"type": "string",
"required": true
},
"stateprov": {
"type": "string",
"required": true
},
"streetaddress1": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"companies": {
"type": "hasMany",
"model": "Companies",
"foreignKey": "companiesid",
"through": "CompaniesAddresses"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": {}
/* ---- companiesaddresses.json ---- */
{
"name": "CompaniesAddresses",
"base": "PersistedModel",
"strict": true,
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"companiesid": {
"type": "number",
"id": true,
"required": true
},
"addressesid": {
"type": "number",
"id": true,
"required": true
}
},
"validations": [],
"relations": {
"addresses": {
"type": "belongsTo",
"model": "Addresses",
"foreignKey": "addressesid"
},
"companies": {
"type": "belongsTo",
"model": "Companies",
"foreignKey": "companiesid"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": {}
}
/* ---- server/boot/model-config.json ---- */
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"Companies": {
"dataSource": "pg",
"public": true,
"$promise": {},
"$resolved": true
},
"Addresses": {
"dataSource": "pg",
"public": true,
"$promise": {},
"$resolved": true
},
"CompaniesAddresses": {
"dataSource": "pg",
"public": true,
"$promise": {},
"$resolved": true
}
}
The foreign keys in companies and addresses are as follows:
**common/models/companies.json**
"relations": {
"addresses": {
"type": "hasMany",
"model": "Addresses",
"foreignKey": "addressesid", <---- this should be companiesid
"through": "CompaniesAddresses"
}
}
On the flip side (addresses) do the same modification, all will work.

AWS cloudfront cli not creating web distribution for custom origin with compress parameter

I was looking to create AWS cloudfront web distribution using CLI, everything was working fine until I tried to add "Compress": True to the distribution's JSON.
Following is my configuration
{
"CallerReference": "string",
"Aliases": {
"Items": [
"test.example.com"
],
"Quantity": 1
},
"DefaultRootObject": "",
"Origins": {
"Items": [
{
"OriginPath": "",
"CustomOriginConfig": {
"OriginProtocolPolicy": "match-viewer",
"HTTPPort": 80,
"HTTPSPort": 443
},
"Id": "Custom-example.com",
"DomainName": "example.com"
}
],
"Quantity": 1
},
"DefaultCacheBehavior": {
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TargetOriginId": "Custom-example.com",
"ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {
"Headers": {
"Quantity": 0
},
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"MaxTTL": 31536000,
"Compress": true,
"SmoothStreaming": false,
"DefaultTTL": 86400,
"AllowedMethods": {
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Items": [
"HEAD",
"GET"
],
"Quantity": 2
},
"Quantity": 2
},
"MinTTL": 0
},
"CacheBehaviors": {
"Quantity": 2,
"Items": [ {
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TargetOriginId": "Custom-example.com",
"ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {
"Headers": {
"Quantity": 0
},
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"MaxTTL": 31536000,
"Compress": true,
"PathPattern": "*.jpeg",
"SmoothStreaming": false,
"DefaultTTL": 86400,
"AllowedMethods": {
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Items": [
"HEAD",
"GET"
],
"Quantity": 2
},
"Quantity": 2
},
"MinTTL": 60
}
,
{
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TargetOriginId": "Custom-example.com",
"ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {
"Headers": {
"Quantity": 0
},
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"MaxTTL": 31536000,
"Compress": true,
"PathPattern": "*.png",
"SmoothStreaming": false,
"DefaultTTL": 86400,
"AllowedMethods": {
"Items": [
"HEAD",
"GET"
],
"CachedMethods": {
"Items": [
"HEAD",
"GET"
],
"Quantity": 2
},
"Quantity": 2
},
"MinTTL": 100
}
]
},
"CustomErrorResponses": {
"Quantity": 0
},
"Comment": "my distro",
"Logging": {
"Bucket": "",
"Prefix": "",
"Enabled": false,
"IncludeCookies": false
},
"PriceClass": "PriceClass_100",
"Enabled": true,
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true,
"MinimumProtocolVersion": "SSLv3"
},
"Restrictions": {
"GeoRestriction": {
"Items": [
"IN",
"GB",
"IR"
],
"RestrictionType": "whitelist",
"Quantity": 3
}
},
"WebACLId": ""
}
This is the error I get
If I remove the compress parameter It works fine.
Parameter validation failed:
Unknown parameter in DistributionConfig.DefaultCacheBehavior: "Compress", must be one of: TargetOriginId, ForwardedValues, TrustedSigners, ViewerProtocolPolicy, MinTTL, AllowedMethods, SmoothStreaming, DefaultTTL, MaxTTL
Unknown parameter in DistributionConfig.CacheBehaviors.Items[0]: "Compress", must be one of: PathPattern, TargetOriginId, ForwardedValues, TrustedSigners, ViewerProtocolPolicy, MinTTL, AllowedMethods, SmoothStreaming, DefaultTTL, MaxTTL
Unknown parameter in DistributionConfig.CacheBehaviors.Items[1]: "Compress", must be one of: PathPattern, TargetOriginId, ForwardedValues, TrustedSigners, ViewerProtocolPolicy, MinTTL, AllowedMethods, SmoothStreaming, DefaultTTL, MaxTTL
After raising a ticket with AWS I got to know that my cloudfront distribution JSON is absolutely fine, the thing which was giving the headache was the CLI version
I was using AWS CLI version 1.9.12 while compress supports only with CLI version >= 1.9.14

AWS CLI create cloudfront distribution: --distribution-config

Trying a cloudfront distribution using the AWS CLI, one needs an argument --distribution-config
aws cloudfront create-distribution
aws: error: argument --distribution-config is required
I assume this is a json string with the configuration for the distribution, but I cannot find any documentation about it.
Where can I find a minimal working example?
The following JSON worked for me. I used get-distribution-config to generate it.
{
"Comment": "example json",
"CacheBehaviors": {
"Quantity": 0
},
"Logging": {
"Bucket": null,
"Prefix": null,
"Enabled": false,
"IncludeCookies": false
},
"Origins": {
"Items": [
{
"S3OriginConfig": {
"OriginAccessIdentity": null
},
"Id": "S3-origin",
"DomainName": "example.s3.amazonaws.com"
}
],
"Quantity": 1
},
"DefaultRootObject": null,
"PriceClass": "PriceClass_All",
"Enabled": false,
"DefaultCacheBehavior": {
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TargetOriginId": "S3-origin",
"ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {
"Headers": {
"Quantity": 0
},
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"SmoothStreaming": false,
"AllowedMethods": {
"Items": [
"GET",
"HEAD"
],
"Quantity": 2
},
"MinTTL": 0
},
"CallerReference": "example",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
},
"CustomErrorResponses": {
"Quantity": 0
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
},
"Aliases": {
"Quantity": 0
}
}
As stated in the documentation you could try to run this command:
aws cloudfront create-distribution --distribution-config file://distconfig.json
The file distconfig.json is a JSON document in the current folder that defines a CloudFront distribution:
distconfig.json
{
"CallerReference": "my-distribution-2015-09-01",
"Aliases": {
"Quantity": 0
},
"DefaultRootObject": "index.html",
"Origins": {
"Quantity": 1,
"Items": [
{
"Id": "my-origin",
"DomainName": "my-bucket.s3.amazonaws.com",
"S3OriginConfig": {
"OriginAccessIdentity": ""
}
}
]
},
"DefaultCacheBehavior": {
"TargetOriginId": "my-origin",
"ForwardedValues": {
"QueryString": true,
"Cookies": {
"Forward": "none"
}
},
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "allow-all",
"MinTTL": 3600
},
"CacheBehaviors": {
"Quantity": 0
},
"Comment": "",
"Logging": {
"Enabled": false,
"IncludeCookies": true,
"Bucket": "",
"Prefix": ""
},
"PriceClass": "PriceClass_All",
"Enabled": true
}
If you follow the link I provided, you will have more information about the output this command will give you.