loopbackjs远程连接器不返回ID

时间:2018-11-01 14:32:19

标签: node.js rest loopbackjs

我有一个nodejs应用程序正在尝试与回送服务器通信

我有以下数据源

{
    "db": {
        "name": "db",
        "connector": "memory"
    },
    "remoteDS": {
        "url": "http://localhost:3033/api",
        "name": "remoteDS",
        "connector": "remote"
    }
}

使用loopback-connector-remote

我有common/models/pack.json

{
    "name": "pack",
    "plural": "packs",
    "base": "PersistedModel",
    "idInjection": true,
    "properties": {},
    "validations": [],
    "relations": {},
    "acls": [],
    "methods": {}
}

和我的test.js

var app = require('./client/client');
app.models.Pack.create({foo: 'bar'})
    .then(result => {
        console.log(result); // { foo: 'bar', id: NaN }
        return app.models.Pack.find()
    })
    .then(result => {
        console.log(result); // [{ foo: 'bar', id: NaN }]
    })
    .catch(err => {
        console.error(err.message);
    })

我不知道为什么id: NaN

当我通过api资源管理器尝试时,我具有ID

[
  {
    "foo": "bar",
    "id": "5bdaf67aed811700149e4549"
  }
]

1 个答案:

答案 0 :(得分:1)

尝试用作您的common/models/pack.json

{
    "name": "pack",
    "plural": "packs",
    "base": "PersistedModel",
    "idInjection": false,
    "properties": {
        "id": {
            "type": "String",
            "id": 1
        }
    },
    "validations": [],
    "relations": {},
    "acls": [],
    "methods": {}
}

环回adds the id property as a number,因此它可能正在尝试在创建时解析您的ID值。也许REST连接器正在创建它并对其进行查询?

  

默认情况下,如果未定义任何ID属性,并且idInjection属性为true(或由于默认为true而未设置),则LoopBack会自动向模型添加一个id属性,如下所示:

     

id:{type:Number,生成的:true,id:true}