2016-07-31 17 views
1

Hallo Ich versuche, dieses Beispiel https://docs.strongloop.com/display/public/LB/HasAndBelongsToMany+relations Ich habe diese beiden Modelle:Beziehungen Loopback

Teil

{ 
    "name": "Part", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "partNumber": { 
     "type": "number" 
    } 
    }, 
    "validations": [], 
    "relations": { 
     "parts": { 
     "type": "hasAndBelongsToMany", 
     "model": "Assembly", 
     "foreignKey": "" 
     } 
    }, 
    "acls": [], 
    "methods": {} 
} 

Assembly

{ 
    "name": "Assembly", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "name": { 
     "type": "string" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "parts": { 
     "type": "hasAndBelongsToMany", 
     "model": "Part", 
     "foreignKey": "" 
    } 
    }, 
    "acls": [], 
    "methods": {} 
} 

Wenn ich versuche hinzufügen Ein Element in Assembly Ich habe ein undefiniertes Element.

Ich verwende

assembly.js

Assembly.parts.add(part, function(err) { 
    ... 
}); 

aber assembly.parts ist nicht definiert.

Was mache ich falsch? Danke

Antwort

2

Sie verwendeten eine statische Methode auf Assembly, die falsch ist.

Relation Methoden sollten als Prototyp-Methoden verwendet werden. Das macht Sinn.

So können Sie wie folgt aufrufen:

assembly_instance.parts.add(...