Loopbackjs 设置userContext环回时为NaN用户ID

Loopbackjs 设置userContext环回时为NaN用户ID,loopbackjs,strongloop,Loopbackjs,Strongloop,我有一个从User扩展而来的UserB2b模型。我已经在启动脚本中使用server.authEnabled启用了授权,以便所有请求都通过auth中间件 问题:每当我的UserB2b模型中有一个非数字用户名时,我就会从loopback/common/model/access token.js中的AccessToken.findForRequest获得一个NaN用户ID 这是我的模型 AccessToken { "name": "AccessToken", "base": "AccessTo

我有一个从
User
扩展而来的
UserB2b
模型。我已经在启动脚本中使用
server.authEnabled
启用了授权,以便所有请求都通过auth中间件

问题:每当我的
UserB2b
模型中有一个非数字用户名时,我就会从
loopback/common/model/access token.js
中的
AccessToken.findForRequest
获得一个
NaN
用户ID

这是我的模型

AccessToken

{
  "name": "AccessToken",
  "base": "AccessToken",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "table": "AccessToken"
  },
  "properties": {
    "ttl": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "ttl",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "created": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "userId": {
      "type": "String",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "userId",
        "dataType": "varchar",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "id": {
      "type": "String",
      "id": true,
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "id",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "model": {
      "type": "string",
      "id": false,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "model",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {
    "userB2B":{
      "type":"belongsTo",
      "model":"UserB2b",
      "foreignKey":"userId"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId":"$authenticated",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}
用户B2B

{
  "name": "UserB2b",
  "base": "User",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "table": "user_b2b"
  },
  "scope": {
    "where": {
      "deleted": 0
    }
  },
  "properties": {
    "username": {
      "type": "String",
      "id": true,
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "username",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "password": {
      "type": "String",
      "required": true,
      "length": 1000,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "password",
        "dataType": "varchar",
        "dataLength": 1000,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "name": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "name",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "contactNumber": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "contact_number",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    },
    "createdAt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created_at",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "updatedAt": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "updated_at",
        "dataType": "timestamp",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "deleted": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 3,
      "scale": 0,
      "mysql": {
        "columnName": "deleted",
        "dataType": "tinyint",
        "dataLength": null,
        "dataPrecision": 3,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "email": false,
    "created": false,
    "lastUpdated": false,
    "credentials": false,
    "challenges": false,
    "status": false,
    "verificationToken": false,
    "realm": false,
    "emailVerified": false
  },
  "validations": [],
  "relations": {
    "accessTokens": {
      "type": "hasMany",
      "model": "Accesstoken",
      "foreignKey": "userName"
    }
  },
  "methods": {}
}
这是我从调试器得到的原始数据包

<-- RowDataPacket
RowDataPacket {
  id: 'BJVqc7CtRXzPVVtTyJqmUabijgLRRHBe3sqcCh0bh5NuGBIZCnY8nwLIGeB6dILv',
  ttl: 12096000,
  created: Tue May 24 2016 05:44:18 GMT+0000 (UTC),
  model: 'UserB2b',
  userId: 'max1' }

这里有什么需要纠正的吗

您的访问令牌中似乎有问题:

"userId": {
  "type": "String",
  "required": false,
  "length": null,
  "precision": 10,
  "scale": 0,
  "mysql": {
    "columnName": "userId",
    "dataType": "int",
使用int并作为varchar项传递的类型,这将引发字符串不是数字(NAN)的异常。 将目标类型更改为string/varchar,或者更改与目标匹配的源数据类型


希望这能对您有所帮助。

对不起,我尝试了不同的方法。我已将其更改为
varchar
,问题仍然存在。您似乎也在尝试扩展内置的AccessToken模型。在这种情况下,您应该以不同的方式命名扩展模型,并让您的环回应用程序知道您将要使用它。还要注意模型定义中的大小写(Accesstoken与Accesstoken、用户名与用户名)。
"userId": {
  "type": "String",
  "required": false,
  "length": null,
  "precision": 10,
  "scale": 0,
  "mysql": {
    "columnName": "userId",
    "dataType": "int",