Phan Phan - 3 months ago 23
Javascript Question

Loopback embedsMany Helper methods don't work

I have these two models:

Student

{
"name": "student",
"plural": "students",
"base": "User",
"idInjection": false,
"options": {
"validateUpsert": true
},
"relations": {
"test": {
"type": "embedsMany",
"model": "test",
"property": "mytest",
"options": {
"validate": true,
"forceId": false
}
}
}


and

Test

{
"name": "test",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"text": {
"type": "string",
"required": true
}
}
}


When I try to create a new test I get this error

Error: Invalid reference: undefined


I create the test in this way:

Student.js

studentInstance.test.add({text : "something "})


What am I doing wrong?

Answer

You don't have test model.

In test.json you defined its names as notification => "name": "notification",

UPDATE

For building (without persisting) an embedded instance please use studentInstance.test.build({text : "something "})

and for creating (with persisting) that please use studentInstance.test.create({text : "something "})