环回使用关系的远程方法

时间:2016-07-12 14:47:10

标签: javascript node.js loopbackjs strongloop

我在项目中使用loopback,并且我有一个与SellerRequests模型相关的MyUser模型(hasMany)。

我看到我现在可以在/ api / MyUsers /:id / sellerRequests上发布一个POST来创建一个链接到用户的新的sellerRequest,但我想做的是在我的common / my-user中使用这个远程方法.js文件。

我尝试做一个MyUser .__ create__sellerRequests,但这是未定义的(对于MyUser.prototype .__ create__sellerRequests和MyUser.createSellerRequests也是如此)。 知道如何访问远程方法吗?

谢谢!

// here is my common/my-user.js file
module.exports = function(Myuser) { 
    console.log(Myuser.__create__sellerRequests); // This is undefined
}

// Here is my MyUser.json
{


"name": "MyUser",
  "base": "User",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "firstname": {
      "type": "string"
    },
    "lastname": {
      "type": "string"
    },
    "gender": {
      "type": "number"
    },
    "birthday": {
      "type": "string"
    },
    "country": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "spokenLanguages": {
      "type": "string"
    },
    "phoneNumber": {
      "type": "string"
    },
    "createdAt": {
      "type": "date",
      "defaultfn": "now"
    }
  },
  "validations": [],
  "relations": {
    "sellers": {
      "type": "hasMany",
      "model": "Seller",
      "foreignKey": "customer_id"
    },
    "sellerRequests": {
      "type": "hasMany",
      "model": "SellerRequest",
      "foreignKey": "customer_id"
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "patchAttributes"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "avatar"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "defaultAvatar"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "avatarUpload"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "avatarDelete"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "__create__sellerRequests"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "__destroyById__sellerRequests"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "__get__sellerRequests"
    },
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "__findById__sellerRequests"
    }
  ],
  "methods": {}
}

1 个答案:

答案 0 :(得分:0)

获取用户实例并使用此

<user instance>.sellerRequests.create(data, function(error, result) {

});

其中data是包含SellerRequest模型的字段和值的对象。

或使用此

MyUser.sellerRequests({customer_id: <user_id>, <rest of the fields in seller requests>}, function(error, result) {

});

参考:Methods available to the model