环回嵌入许多辅助方法不起作用

Loopback embedsMany Helper methods don't work

我有这两个型号:

学生

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

测试

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

当我尝试创建新测试时出现此错误

Error: Invalid reference: undefined

我是这样创建测试的:

Student.js

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

我做错了什么?

更新

在 embedsMany 中删除

在测试中更新 id。

Student.js

Student.show = function(email, cb) {


        Student.findById(email,function(err, student) {

            ...

            var tmp = student.mytest;

            for (var i = 0; i < tmp.length; i++) {
                student.test.destroy(tmp[i].id);
            }

        })

               ...

    }

我试过

destroy工作不正常,不总是删除数据

remove 显示此错误

Error: Invalid reference: undefined
    at EmbedsMany.remove

更新

添加了一个数据库示例

{
    "_id": "value",
    "property1": "value",
    .
    .

    "mytest": [
        {
            "text": "something",
            "creation": {
                "$date": "2016-08-23T14:31:44.678Z"
            },
            "id": "d738253472876b17feb4b46b"
        }
    ]
}

您没有 test 模型。

test.json 中,您将其名称定义为 notification => "name": "notification",

更新

要构建(不持久化)嵌入式实例,请使用 studentInstance.test.build({text : "something "})

要创建(持久化)请使用 studentInstance.test.create({text : "something "})

3 年后,我在使用 remove, destroyById 时遇到了同样的问题。

然后我使用unset方法。有效。

Person.findById(body.metadata.person.id, (err, person) => {
    person.creditCards.unset(
        body.creditCard.id,
        (err, res) => {
        }
    );
}

这个效果很好。