0

I'm trying StrongLoop Loopback replication with two MongoDB datasources. After posting some data in the Phone database, this error shows up.

TypeError: Cannot call method 'getChangeModel' of undefined at Function.PersistedModel.replicate (/home/dchavez/Media/projects/SyncTest/node_modules/loopback/lib/models/persisted-model.js:726:34)

The data is being saved in the Phone collecion, but it's not being replicated to the Repo collection due the error.

These are my model configurations.

Phone Model

{
  "name": "Phone",
  "plural": "Phones",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "options": {
    "trackChanges": true
  },
  "properties": {
    "id": {
      "type": "string",
      "id": true
    },
    "num": {
      "type": "string"
    },
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

Repo Model

{
  "name": "Repo",
  "plural": "Repos",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string"
    },
    "num": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

My datasource configuration is this:

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "MongoDb": {
    "host": "localhost",
    "port": 27017,
    "database": "PhoneDb",
    "name": "MongoDb",
    "connector": "mongodb"
  }}

The Phone,js is this

module.exports = function(Phone) {
  var loopback = require('../..');
  var Repo = loopback.Repo;
  var conflicts;

  Phone.on('changed', function(obj) {
    replicateSourceToTarget();
    resolveAllConflicts();
  });

  function replicateSourceToTarget() {
    Phone.replicate(0, Repo, {}, function(err, replicationConflicts) {
      conflicts = replicationConflicts;
    });
  }

  function resolveAllConflicts() {
    if (conflicts.length) {
      conflicts.forEach(function(conflict) {
        conflict.resolve();
      });
    }
  }
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What are the settings in your model-config.js files (client/server) ? The code you shared is for the server model or client ? – Pandaiolo Oct 09 '14 at 09:15
  • { "_meta": { "sources": [ "../common/models", "./models" ] }, "User": { "dataSource": "db" }, "AccessToken": { "dataSource": "db", "public": false }, "ACL": { "dataSource": "db", "public": false }, "RoleMapping": { "dataSource": "db", "public": false }, "Role": { "dataSource": "db", "public": false }, "Phone": { "dataSource": "MongoDb", "public": true }, "Repo": { "dataSource": "MongoDb", "public": true } } – Escafoid2000 Oct 10 '14 at 15:52
  • The code is for the server model – Escafoid2000 Oct 10 '14 at 15:53

0 Answers0