ReferenceError: g is not defined loopback extended models - loopbackjs

I am having a nearly identical problem to my previous question. When a user model is created, the following error is returned:
ReferenceError: g is not defined
at new ModelConstructor (eval at createModelClassCtor (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model-builder.js:671:21), <anonymous>:10:27)
at user.ModelBaseClass._initProperties (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model.js:349:28)
at user.ModelBaseClass (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model.js:60:8)
at user.Model (eval at createModelClassCtor (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model-builder.js:671:21), <anonymous>:12:24)
at user.PersistedModel (eval at createModelClassCtor (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model-builder.js:671:21), <anonymous>:12:24)
at user.User (eval at createModelClassCtor (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model-builder.js:671:21), <anonymous>:12:24)
at new user (eval at createModelClassCtor (/usr/src/app/node_modules/loopback-datasource-juggler/lib/model-builder.js:671:21), <anonymous>:12:24)
at Function.DataAccessObject.create (/usr/src/app/node_modules/loopback-datasource-juggler/lib/dao.js:359:13)
at /usr/src/app/node_modules/loopback-datasource-juggler/lib/dao.js:1262:13
at /usr/src/app/node_modules/loopback-datasource-juggler/lib/dao.js:2175:62
at /usr/src/app/node_modules/loopback-datasource-juggler/lib/dao.js:2111:9
at /usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:1012:9
at /usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:359:16
at eachOfArrayLike (/usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:928:9)
at eachOf (/usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:990:5)
at _asyncMap (/usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:1005:5)
at Object.map (/usr/src/app/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:995:16)
at allCb (/usr/src/app/node_modules/loopback-datasource-juggler/lib/dao.js:2025:13)
at /usr/src/app/node_modules/loopback-connector-mongodb/lib/mongodb.js:1155:9
at result (/usr/src/app/node_modules/mongodb/lib/utils.js:414:17)
at executeCallback (/usr/src/app/node_modules/mongodb/lib/utils.js:406:9)
at handleCallback (/usr/src/app/node_modules/mongodb/lib/utils.js:128:55)
Here are my models:
user.json:
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type" : "string",
"id" : true,
"required" : true,
"defaultFn" : "guid"
},
"type": {
"type" : "[string]",
"required" : true,
"default" : ["student"]
},
"full_name": {
"type": "string",
"required": false
},
"office" : {
"type": "string",
"required" : false
},
"profile_img": {
"type": "string",
"required": false
},
"departmentId": {
"type": "string",
"required": false
}
},
"validations": [],
"relations": {
"department": {
"type": "belongsTo",
"model": "department"
},
"syncedcalendar" : {
"type" : "hasMany",
"model" : "syncedcalendar"
},
"accessTokens": {
"type": "hasMany",
"model": "accessToken",
"foreignKey": "userId",
"options": {
"disableInclude": true
}
}
},
"acls": [],
"methods": {}
}
accessToken.json
{
"name": "accessToken",
"plural": "accessTokens",
"base": "AccessToken",
"properties": {},
"validations": [],
"idInjection": false,
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
},
"acls": [],
"methods": []
}

The error above (in both this case and my previous issue) resulted because loopback could not parse the model's json file. In this case, it was the type property of user:
"type": {
"type" : "[string]",
"required" : true,
"default" : ["student"]
},
It seems the default cannot be an array. Removing this default and enforcing this default in a hook resolved the issue.

I went through an issue that resembled yours. And yes, the error stack gives too little useful info. It should instead say: "please stick to Loopback types" because that's the problem. You're stuck with Loopback types.
In your case, the parser couldn not handle an array. Well, it turns out it won't parse an un recognized type string either. For example, if you try to declare a property as integer:
"id":
{
"type": "Integer", <----- not a loopback type
"id": 1,
"mysql":
{
"columnName": "id",
"dataType": "int",
}
},
It will throw, because the native type is Number. You can format it as integer, but don't mess with its type.
"id":
{
"type": "Number", <---- one of loopback types. Integer is not one of them
"id": 1,
"mysql":
{
"columnName": "id",
"dataType": "int",
}
"format":"integer" <---- here's where you can format or cast as desired
},
If your database values are json-like (so they should rehidrate as arrays and objects) just declare them as Objects.

Related

correct way to define Loopback.io belongsTo relationship

I have a simple has_many-belongst_to relationship between 2 Loopback models:
{
"name": "ApiUser",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"table": "users"
},
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"id": true,
"required": true,
"defaultFn": "uuid",
"postgresql": {
"dataType": "uuid"
}
},
"email": {
...
and
{
"name": "ShopifyAccount",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"api_key": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"orders": {
"type": "hasMany",
"model": "ShopifyOrder",
"foreignKey": ""
},
"user": {
"type": "belongsTo",
"model": "ApiUser",
"foreignKey": "userid"
}
},
"acls": [],
"methods": {}
}
When I run automigration, the shopifyaccount table is created, but it looks weird:
Column | Type | Modifiers
-------------------+---------+-------------------------------------------------------------
api_key | text | not null
password | text | not null
id | integer | not null default nextval('shopifyaccount_id_seq'::regclass)
userid | uuid |
apiuserid | uuid |
Indexes:
"shopifyaccount_pkey" PRIMARY KEY, btree (id)
Why did it create 2 columns named like this? Even if I try to specify that foreignkey is "userid", it will still create the apiuserid column. The insert will never update the apiuserid column but it will update the userid column. And then at join time, it will try to join on apiuserid. What am I doing wrong?
So... it seems that the "foreignkey" needs to be specified in both places, in ApiUser and in ShopifyAccount:
{
"name": "ApiUser",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"table": "users"
},
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"id": true,
"required": true,
"defaultFn": "uuid",
"postgresql": {
"dataType": "uuid"
}
},
"email": {
"type": "string",
...
"relations": {
"shopifyAccounts": {
"type": "hasOne",
"model": "ShopifyAccount",
"foreignKey": "userid"
}
},
"acls": [],
"methods": {}
}

Loopback hasManyThrough relation for user

I have such models:
Team
{
"name": "Team",
"plural": "teams",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"mixins": {
"ModelRest": {}
},
"hidden": [
"deleted"
],
"filtered": [
"userId",
"archived"
],
"properties": {
"name": {
"type": "string",
"required": true
},
"createdAt": {
"type": "date"
},
"deleted": {
"type": "boolean"
}
},
"validations": [],
"relations": {
"projects": {
"type": "hasMany",
"model": "Project"
},
"user": {
"type": "belongsTo",
"model": "user"
},
"users": {
"type": "hasMany",
"model": "User",
"foreignKey": "",
"through": "TeamMember"
}
},
"acls": [],
"methods": {}
}
TeamMember
{
"name": "TeamMember",
"plural": "team-members",
"base": "Model",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "user"
},
"team": {
"type": "belongsTo",
"model": "Team"
}
},
"acls": [],
"methods": {}
}
user
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"mixins": {
"ModelRest": {}
},
"hidden": [
"realm",
"emailVerified",
"lastIP",
"deleted",
"utmSource",
"utmMedium",
"utmCampaign"
],
"readOnly": [
"statusId",
"lastListId",
"teamId",
"subscriptionStart",
"subscriptionExpiration",
"apiKey"
],
"properties": {
"name": {
"type": "string",
"required": true,
"mysql": {
"columnName": "username"
}
},
"password": {
"type": "string",
"required": true,
"min": 5
},
"email": {
"type": "string",
"required": true
},
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
},
"subscriptionStart": {
"type": "date"
},
"subscriptionExpiration": {
"type": "date"
},
"teamId": {
"type": "number"
},
"sharePlan": {
"type": "boolean"
},
"shareLeads": {
"type": "boolean"
},
"timezone": {
"type": "string"
},
"utmSource": {
"type": "string"
},
"utmMedium": {
"type": "string"
},
"utmCampaign": {
"type": "string"
},
"lastIP": {
"type": "string"
},
"deleted": {
"type": "boolean"
}
},
"validations": [],
"relations": {
"team": {
"type": "belongsTo",
"model": "Team"
},
"plan": {
"type": "belongsTo",
"model": "Plan"
},
"billingCycle": {
"type": "belongsTo",
"model": "BillingCycle"
},
"card": {
"type": "belongsTo",
"model": "Card"
},
"lastList": {
"type": "belongsTo",
"model": "List"
},
"status": {
"type": "belongsTo",
"model": "Status"
},
"accessTokens": {
"type": "hasMany",
"model": "AccessToken"
}
},
"acls": [],
"methods": {}
}
I've created relation in Team model:
"users": {
"type": "hasMany",
"model": "User",
"foreignKey": "",
"through": "TeamMember"
}
But users relation in Team doesn't work at all. In API explorer I see
GET /teams/{id}/user
there is no
GET /teams/{id}/users
Why does this happen?
I've even created this relation with Loopback relation generator. Same result. Can't figure out where is the error. Loopback doest see this relation.
Thanks for any help.
It's all, because in model-config.json I have
"TeamMember": {
"dataSource": null,
"public": true
},
instead of:
"TeamMember": {
"dataSource": "db",
"public": true
},

Loopback: How to define a property with an array of strings in Loopback?

I have the following model in a loopback application, that will be persisted in a MongoDB:
Model
Name Coffeshop:
Id
Name (string)
City (String)
Question:
Now i want to be able to store a list of strings in a new property called "tags":
Tags (Array of string)
There is no relation to other models necessary. I need just a plain flat list of strings.
How can i achieve this?
Code:
{
"name": "CoffeeShop",
"plural": "CoffeeShops",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Thats easy:
{
"name": "CoffeeShop",
"plural": "CoffeeShops",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
},
"tags": {
"type": [
"string"
],
"required": false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

Dual belongsTo relations with loopback

I have a couple relations I want to model,
A Message belongs to 2 Profile's (a sender and a recipient)
An Enrollment consists of a Profile and a Curriculum.
I tried to do #1 using 2 hasOne but ended up with Profile.messageId.
{
"name": "Message",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number",
"id": true,
"required": true
},
"text": {
"type": "string",
"required": true
},
"created": {
"type": "date",
"required": true
},
"seen": {
"type": "boolean",
"required": true
}
},
"validations": [],
"relations": {
"sender": {
"type": "hasOne",
"model": "Profile",
"foreignKey": ""
},
"recipient": {
"type": "hasOne",
"model": "Profile",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
Same problem w/ #2...
{
"name": "Enrollment",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number",
"id": true,
"required": true
},
"created": {
"type": "date",
"required": true
},
"currentPage": {
"type": "string",
"comments": "What page are they on in this curriculum?"
}
},
"validations": [],
"relations": {
"curriculums": {
"type": "hasOne",
"model": "Curriculum",
"foreignKey": ""
},
"profiles": {
"type": "hasOne",
"model": "Profile",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}

Createmany in Strongloop Loopback

I have an Order model which hasMany OrderItem models. But once a client wants to create an Order, it has to create an Order object first then for each product he added to his basket, he needs to create responding OrderItems separately. As you may notice it causes many reduntant requests. May be I can make a custom method for OrderItems which consumes a product list. But i was wondering if there is a built in mechanism for this like createMany since it is a very useful operation.
ORDER MODEL
{
"name": "Order",
"plural": "Orders",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"customerId": {
"type": "number",
"required": true
},
"branchId": {
"type": "number",
"required": true
}
},
"validations": [],
"relations": {
"orderItems": {
"type": "hasMany",
"model": "OrderItem",
"foreignKey": "orderId"
}
},
"acls": [],
"methods": []
}
ORDERITEM MODEL
{
"name": "OrderItem",
"plural": "OrderItems",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"UnitPrice": {
"type": "number"
},
"productId": {
"type": "number",
"required": true
},
"purchaseOrderId": {
"type": "number",
"required": true
},
"quantity": {
"type": "number"
}
},
"validations": [],
"relations": {
"product": {
"type": "belongsTo",
"model": "Product",
"foreignKey": "productId"
},
"purchaseOrder": {
"type": "belongsTo",
"model": "PurchaseOrder",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
Loopback "create" method accepts also an array of objects (see PersistedModel.create docs) so you should try creating one "create" call and send an array of OrderItems.