Loopback: Embedded Model is not working in offline sync
Asked Answered
C

1

10

I have followed the loopback offline sync example and create my own model with embedded document. I created a Model named Project where ProjectMembers are embedded model. Here are my model:
Project.json

{
    "name": "Project",
    "base": "PersistedModel",
    "strict": "throw",
    "persistUndefinedAsNull": true,
    "trackChanges": true,
    "properties": {
        ...
     },
    "relations": {
       "members": {
            "type": "embedsMany",
            "model": "ProjectMember",
            "property": "members",
            "options": {
               "validate": true,
               "forceId": false
            }
          }
    }
}

ProjectMember.json

{
    "name": "ProjectMember",
    "base": "Model",
    "idInjection": true,
    "properties": {
        ...
    },
    "validations": [],
    "relations": {},
    "acls": [],
    "methods": []
}

In the server side model-config.json I updated the datasource as below:

"Project": {
    "dataSource": "my_db"
},
"ProjectMember": {
    "dataSource": "transient"
}

And in the client side in lbclient/models/ I added 2 files local-project.json and remote-project.json as exactly same as local-todo.json and remote-todo.json. I updated the client side model-config.json file as below:

"RemoteProject": {
    "dataSource": "remote"
},
"LocalProject": {
    "dataSource": "local"
}

In the client controller I run the following codes:

ProjectModel.create($scope.project)
.then(function(project) {
    var owner = loginDetails.getLoginUser();// the member
    owner.role = 'owner';
    owner.status = 'active';
    project.members.create(owner); //shows error: couldn't read property  

    $scope.project = {};
    $scope.$apply();
});

It creates the Project but failed to create the embedded model. It displays "Couldn't read property create undefined"? Is there any way to create embedded model in the client side?

UPDATE
The embedded model works only on server side. But when the browserify create the browse.bundle.js, it fails to add the embedded model.

Criminality answered 11/10, 2015 at 12:34 Comment(2)
can you share the error log for "Couldn't read property create undefined"?Rickyrico
here is the error log: Uncaught (in promise) TypeError: Cannot read property 'create' of undefinedCriminality
C
0

I followed the trial and error method and has come to a solution about offline embedded model. As previously,I only defined the relations between Project and ProjectMember in the "common/models/" directory. What I find is, I have to define the relations in the client side model too. So I did the following steps and it works.

  1. I created a json file - "lbclient/models/local-project-member.json".

    {
      "name": "LocalProjectMember",
      "base": "ProjectMember"
    }
    
  2. Added the following lines in "lbclient/model-config.json".

     "LocalProjectMember":{
        "dataSource": "local"
      }
    
  3. Modified the "lbclient/models/local-project.json" file as below.

    {
     "name": "LocalProject",
     "base": "Project",
     "relations":{
    
       "members": {
         "type": "embedsMany",
         "model": "LocalProjectMember",
         "property": "memberList",
         "options": {
           "persist": true,
           "validate": true,
           "forceId": false
         }
       }
     }
    }  
    

So the conclusion is if you want your embedded model work on offline you have to redefine the relations in the client side model.

Criminality answered 21/10, 2015 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.