Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loopbackjs Loopback.io位于REST中_Loopbackjs_Strongloop - Fatal编程技术网

Loopbackjs Loopback.io位于REST中

Loopbackjs Loopback.io位于REST中,loopbackjs,strongloop,Loopbackjs,Strongloop,我对loopback.io的belongsTo关系函数有点困惑 因此,让我们以以下示例为例: 我有一个名为Project的模型,它与客户对象有关。因此,项目属于客户 这是我的项目模型 { "name": "Project", "plural": "Projects", "base": "PersistedModel", "strict": false, "idInjection": false, "options": { "validateUpsert": tru

我对loopback.io的belongsTo关系函数有点困惑

因此,让我们以以下示例为例:

我有一个名为Project的模型,它与客户对象有关。因此,项目属于客户

这是我的项目模型

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
因此,当我运行应用程序并转到时,我可以看到API。但当我进入项目时,我只看到

 GET /Projects/{id}/customer     Fetches belongsTo relation customer.
我还期待其他功能,如

 POST /Projects/{id}/customer 
 DELETE /Projects/{id}/customer 

为什么这里没有?或者如何通过REST API为项目设置客户?

据我所知,它不是这样工作的。为了创建链接,您需要创建客户,然后使用该客户的id作为项目模型中的id

因此,您的API调用将是:

邮政/客户

创建客户,然后

员额/项目

创建项目

在第二篇文章中,您需要指定CustomerId以将项目链接到客户,或者您需要在事后使用CustomerId更新项目

然后:

项目/{id}/客户

检索属于项目的客户

或者,您可以在其中一个模型上编写自己的远程方法,以便在一次调用中执行此操作

可以在此处找到关系API的完整列表:


据我所知,它不是那样工作的。为了创建链接,您需要创建客户,然后使用该客户的id作为项目模型中的id

因此,您的API调用将是:

邮政/客户

创建客户,然后

员额/项目

创建项目

在第二篇文章中,您需要指定CustomerId以将项目链接到客户,或者您需要在事后使用CustomerId更新项目

然后:

项目/{id}/客户

检索属于项目的客户

或者,您可以在其中一个模型上编写自己的远程方法,以便在一次调用中执行此操作

可以在此处找到关系API的完整列表:


首先,您的项目模型缺少您为belongsTo关系提到的customerId字段

这是项目模型

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    },
    "customerId": {
      "type": "string",
      "required":true
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
{
  "name": "Customer",
  "plural": "Customers",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string",
      "required": true
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "projects": {
      "type": "hasMany",
      "model": "Project",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
现在,您的客户模型还应该具有关系的其他部分。没有这一点,它就不会像预期的那样工作

以下是客户型号的代码

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    },
    "customerId": {
      "type": "string",
      "required":true
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
{
  "name": "Customer",
  "plural": "Customers",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string",
      "required": true
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "projects": {
      "type": "hasMany",
      "model": "Project",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
现在定义了客户项目关系的双方,您的API应该有如下端点:

GET Customers/{id}/Projects
POST Customers/{id}/Projects
PUT Customers/{id}/Projects

您提到的端点无效,因为您不能为Project创建客户,但可以通过其他方式创建客户

首先,您的项目模型缺少您为belongsTo关系提到的customerId字段

这是项目模型

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    },
    "customerId": {
      "type": "string",
      "required":true
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
{
  "name": "Customer",
  "plural": "Customers",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string",
      "required": true
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "projects": {
      "type": "hasMany",
      "model": "Project",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
现在,您的客户模型还应该具有关系的其他部分。没有这一点,它就不会像预期的那样工作

以下是客户型号的代码

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "dateCreated": {
      "type": "date"
    },
    "customerId": {
      "type": "string",
      "required":true
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
{
  "name": "Customer",
  "plural": "Customers",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string",
      "required": true
    },
    "dateCreated": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {
    "projects": {
      "type": "hasMany",
      "model": "Project",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}
现在定义了客户项目关系的双方,您的API应该有如下端点:

GET Customers/{id}/Projects
POST Customers/{id}/Projects
PUT Customers/{id}/Projects

您提到的端点无效,因为您不能为Project创建客户,但可以通过其他方式创建客户

让我们首先了解的关系,了解为什么只创建GET rest端点:

GET /Projects/{id}/customer     Fetches belongsTo relation customer.
以及为什么Loopback没有创建以下链接:

POST /Projects/{id}/customer 
DELETE /Projects/{id}/customer
belongsTo
关系在两个模型实例之间创建一对一的关系。它用于将一个实例的所有权提供给另一个实例。在您的情况下,项目模型实例属于客户模型实例。现在,由于项目属于客户,所以客户拥有项目实例,所以对其余端点的解释也是如此

GET /Projects/{id}/customer     Fetches belongsTo relation customer.
因为客户可以有项目实例,所以上面的内容是有效的,因为客户可以为项目获取

POST /Projects/{id}/customer
DELETE /Projects/{id}/customer

由于客户不属于某个项目,而是拥有一个项目,因此上述rest端点在创建或否认其所有者(客户)的项目中没有意义。

让我们首先了解之间的关系,了解为什么只创建GET rest端点:

GET /Projects/{id}/customer     Fetches belongsTo relation customer.
以及为什么Loopback没有创建以下链接:

POST /Projects/{id}/customer 
DELETE /Projects/{id}/customer
belongsTo
关系在两个模型实例之间创建一对一的关系。它用于将一个实例的所有权提供给另一个实例。在您的情况下,项目模型实例属于客户模型实例。现在,由于项目属于客户,所以客户拥有项目实例,所以对其余端点的解释也是如此

GET /Projects/{id}/customer     Fetches belongsTo relation customer.
因为客户可以有项目实例,所以上面的内容是有效的,因为客户可以为项目获取

POST /Projects/{id}/customer
DELETE /Projects/{id}/customer

因为客户不属于某个项目,而是拥有一个项目,所以当项目创建或否认其所有者(客户)时,上面的rest端点就没有意义了。

“客户拥有项目”解释了这一切。我将在项目侧放置一个belongsTo,在客户侧放置一个hasMany。这将完成双方的关系。“客户拥有项目”解释了这一切。我将在项目侧放置一个belongsTo,在客户侧放置一个hasMany。这将完成双方的关系。