loopback icon indicating copy to clipboard operation
loopback copied to clipboard

embedsMany helper methods not defined/accessible

Open TrySpace opened this issue 10 years ago • 7 comments

Whatever I try, I cannot get the helper methods working like described here: https://docs.strongloop.com/display/public/LB/Embedded+models+and+relations#Embeddedmodelsandrelations-EmbedsMany

node.js:

  Node.remoteMethod (
      'pushMovements',
      {
        isStatic: false,
        http: {path: '/pushMovements', verb: 'post'},
        accepts: [
          { arg: 'data', type: 'array', http: { source: 'body' } }
        ],
        returns: {arg: 'put', type: 'string'},
        description: 'pushMovements'
      }
  );

  Node.prototype.pushMovements = function(revision, cb) {
    console.log('id:',this.id)
    console.log('rev:',revision)
     revision.forEach(function (result) {     
       Node.movements.add(result)    
      });
     cb(null, revision)
   };

node.json:

{
  "name": "Node",
  "plural": "Nodes",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "type": "string"
    },
    "status": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "movements": {
      "type": "embedsMany",
      "model": "Movement",
      "foreignKey": "nodeId",
      "keyThrough": "movementId",
      "through": "Movement",
      "property": "movementData",
      "options": {
          "validate": true,
          "autoId": true
      }
    }
  },
  "acls": [],
  "methods": {}
}

movment.json:

{
  "name": "Movement",
  "plural": "Movements",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "nodeId": {
      "type": "String"
      },
    "ts": {
      "type": "Date"
    },
    "battery": {
      "type": "String"
    },
    "movement": {
      "type": "String"
    }
  },
  "validations": [],
  "relations": {
    "node": {
      "type": "belongsTo",
      "model": "Node",
      "foreignKey": "nodeId"
    }
  },
  "acls": [],
  "methods": {}
}

Whenever I post: [ {"id" : "xyz" }, {"id" : "zyx" } ] to /Nodes/{id}/pushMovements it outputs:

id: 56b351934a286a0820b50c1e
rev: [ { id: 'xyz' }, { id: 'zyx' } ]
[TypeError: Cannot read property 'add' of undefined]

I've tried all kinds of combinations of foreignKeys and with or without through etc, it just won't work... I've been at this for several days now :'(

I've also cloned: https://github.com/strongloop/loopback-example-relations and indeed .create works (https://github.com/strongloop/loopback-example-relations/blob/master/server/boot/z-customer-emails.js) , but only in the bootscript but not in the method I'm using.

(I posted A similar question on stackoverflow: https://stackoverflow.com/questions/35081213/strongloop-embedsmany-helper-methods-not-found i've put a bounty on it for 100 rep points ;p...)

TrySpace avatar Feb 04 '16 14:02 TrySpace

Hi @TrySpace Do you have a link for your own repo? It is better if I test the error there. Thanks

loay avatar Feb 11 '16 03:02 loay

Hi, I've the same problem. I'm making unit tests and I want to populate some fake data but I've this problem when I'm adding objects in a relation "referencesMany".

I'm not sure if I'm missing something in the initialisation of my tests :

var loopback = require('loopback');
var app = require('../server/server');
var ds = loopback.createDataSource('memory');
var Filter = ds.createModel('Filter', app.models.Filter);
var Package = ds.createModel('Package', app.models.Package);

So a Package has this relation :

"relations": {
    "filters": {
      "type": "referencesMany",
      "model": "Filter",
      "property": "filtersIds",
      "options": {
        "forceId": false,
        "validate": true
      }
    }
  }

Thanks for your help

cyrilclo avatar Feb 11 '16 12:02 cyrilclo

@cookinclude I would be more than happy to help. Would it be possible to refer me to a link to your repo? Here is how to report a bug : please use this one as a reference? Thanks. https://github.com/strongloop/loopback/wiki/Reporting-issues#bug-report

loay avatar Feb 11 '16 18:02 loay

Hi, I am closing this issue for now. If you are still experiencing the same problem, please feel free to leave a comment and I will re-open the issue. Thanks.

loay avatar Apr 25 '16 21:04 loay

Sorry but I have to request to reopen this issue, I have the same problem, and for me if the answer is not posted the issue cannot be closed. Please post some hints about this issue, how to solve it or how to woraround it. I have exactly the same problem, I have declared an embedsmany relation and no matter what I do I get this:

Error: Invalid reference: undefined
    at EmbedsMany.add (/var/www/FOLDER/node_modules/loopback-datasource-juggler/lib/relation-definition.js:2935:11)
    at ModelConstructor.method [as __link__sentinelPlans] (/var/www/sentineltrack/node_modules/loopback-datasource-juggler/lib/relation-definition.js:732:33)

hackerunet avatar Aug 10 '16 01:08 hackerunet

I'm having the same problem with the .remove() and the .destroyById(). According to the documentation these helper-methods should be available for embedded relations but I'm getting error messages.

I've forked the loopback-example-relations repository to illustrate the problems: https://github.com/frankorama/loopback-example-relations

To run the example, start the loopback application.

Navigate to

http://0.0.0.0:3000/email

Each email address will have a remove link which calls a remoteMethod I created. The code is commented and located in the /common/models/customer.js file

.remove() returns the following error

//  Error: Invalid reference: undefined
//    at EmbedsMany.remove (/Users/francoislaforge/GitSource/loopback-examples/loopback-example-relations/node_modules/loopback-datasource-juggler/lib/relation-definition.js:2984:11)
//    at ModelConstructor.method [as __unlink__emails] (/Users/francoislaforge/GitSource/loopback-examples/loopback-example-relations/node_modules/loopback-datasource-juggler/lib/relation-definition.js:733:33)

.destroyById() returns the following error

 // TypeError: customer.emails.destroyById is not a function

frankorama avatar Nov 07 '16 15:11 frankorama

@TrySpace I know this is old, but I just had another look at your code. You are attempting to add a new child instance (result) to the parent model (Node). You need to add the children to a parent instance

Change your code as follows:

  Node.prototype.pushMovements = function(revision, cb) {
    console.log('id:',this.id)
    console.log('rev:',revision)
    Node.findById(parentId, function(parentFindErr, parentNode){
       revision.forEach(function (result) {     
         parentNode.movements.add(result)    
        });
       cb(null, revision)
    });
   };

frankorama avatar Feb 25 '17 10:02 frankorama