Loopback 使用关系的远程方法
Loopback use a relation's remote method
我在我的项目中使用环回,并且我有一个与 SellerRequests 模型相关 (hasMany) 的 MyUser 模型。
我看到我现在可以在 /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": {}
}
获取用户实例并使用它
<user instance>.sellerRequests.create(data, function(error, result) {
});
其中数据是包含 SellerRequest
模型的字段和值的对象。
或使用这个
MyUser.sellerRequests({customer_id: <user_id>, <rest of the fields in seller requests>}, function(error, result) {
});
我在我的项目中使用环回,并且我有一个与 SellerRequests 模型相关 (hasMany) 的 MyUser 模型。
我看到我现在可以在 /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": {}
}
获取用户实例并使用它
<user instance>.sellerRequests.create(data, function(error, result) {
});
其中数据是包含 SellerRequest
模型的字段和值的对象。
或使用这个
MyUser.sellerRequests({customer_id: <user_id>, <rest of the fields in seller requests>}, function(error, result) {
});