Loopback embedsMany Helper方法不起作用

时间:2016-08-10 17:24:28

标签: javascript loopbackjs strongloop

我有这两个模型:

学生

{
    "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

更新

添加了db

的示例
{
    "_id": "value",
    "property1": "value",
    .
    .

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

2 个答案:

答案 0 :(得分:2)

您没有test型号。

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

<强>更新

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

并创建(使用持久性),请使用studentInstance.test.create({text : "something "})

答案 1 :(得分:0)

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

然后我使用unset方法。可以。

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

这很好。