ashishSober
ashishSober

Reputation: 1251

My loopback model-relation is not working

As I created my user model on top of User-built in model and created a new model library. But if I'm trying to make a relationship its giving the error "unauthorized".

Here are my files:

user.json

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "phone": {
      "type": "string"
    },
    "age": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "library": {
      "type": "hasMany",
      "model": "Library",
      "foreignKey": ""
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

and my library.json

{
  "name": "Library",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "category": {
      "type": "string"
    },
    "bookTitle": {
      "type": "string"
    },
    "author": {
      "type": "string",
      "required": true,
      "index": false
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": ""
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

As you could see that foreign key is empty what exactly it means I don't know why we need to left empty that and user login is working fine. It returns me the access token which I'm setting at the top right.

I didn't find the appropriate answer as I'm saving my database to MongoDB.

Upvotes: 0

Views: 355

Answers (1)

GiggleGirl
GiggleGirl

Reputation: 77

please try this add in library.json

"acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }
  ]

and in user.json file add these

"acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    },{
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"

    }
  ],

u need to give the owner permission for both the models. Please let me know if you are facing any issue

Upvotes: 2

Related Questions