2017-06-12 46 views
0

我有2種型號回送3 「hasOne」 關係

MerchantModel

{ 
 
    "name": "Merchant", 
 
    "plural": "Merchants", 
 
    "base": "PersistedModel", 
 
    "idInjection": true, 
 
    "options": { 
 
    "validateUpsert": true, 
 
    "mysql": { 
 
     "table": "tbl_merchants" 
 
    } 
 
    }, 
 
    "properties": { 
 
    "merchant_id": { 
 
     "type": "string", 
 
     "id": true, 
 
     "defaultFn": "uuidv4" 
 
    }, 
 
    "owner_id": { 
 
     "type": "string", 
 
     "id": true, 
 
     "length": 36 
 
    }, 
 
    "merchant_name": { 
 
     "type": "string", 
 
     "required": true, 
 
     "length": 100 
 
    }, 
 
    "address": { 
 
     "type": "string" 
 
    }, 
 
    "phone_number": { 
 
     "type": "string", 
 
     "length": 50 
 
    }, 
 
    "email": { 
 
     "type": "string", 
 
     "required": true, 
 
     "length": 100 
 
    }, 
 
    "status": { 
 
     "type": "number", 
 
     "length": 1 
 
    }, 
 
    "notify_queue_no": { 
 
     "type": "number", 
 
     "length": 1 
 
    }, 
 
    "subscription_status": { 
 
     "type": "number", 
 
     "length": 1 
 
    }, 
 
    "merchant_category_id": { 
 
     "type": "string", 
 
     "length": 36 
 
    }, 
 
    "merchant_type_id": { 
 
     "type": "string", 
 
     "length": 36 
 
    }, 
 
    "merchant_link": { 
 
     "type": "string", 
 
     "length": 100 
 
    }, 
 
    "owner_id": { 
 
     "type": "string", 
 
     "length": 36 
 
    } 
 
    }, 
 
    "validations": [], 
 
    "relations": { 
 
    "user": { 
 
     "type": "hasOne", 
 
     "model": "UserData", 
 
     "foreignKey": "user_id", 
 
     "primaryKey": "owner_id" 
 
    }, 
 
    "items": { 
 
     "type": "hasMany", 
 
     "model": "Item", 
 
     "foreignKey": "merchant_id" 
 
    }, 
 
    "item_categories": { 
 
     "type": "hasMany", 
 
     "model": "ItemCategory", 
 
     "foreignKey": "merchant_id" 
 
    } 
 
    }, 
 
    "acls": [ 
 
    { 
 
     "accessType": "*", 
 
     "principalType": "ROLE", 
 
     "principalId": "$everyone", 
 
     "permission": "DENY" 
 
    }, 
 
    { 
 
     "accessType": "*", 
 
     "principalType": "ROLE", 
 
     "principalId": "$authenticated", 
 
     "permission": "ALLOW" 
 
    }, 
 
    { 
 
     "accessType": "*", 
 
     "principalType": "ROLE", 
 
     "principalId": "$everyone", 
 
     "permission": "DENY" 
 
    }, 
 
    { 
 
     "accessType": "READ", 
 
     "principalType": "ROLE", 
 
     "principalId": "merchantOwner", 
 
     "permission": "ALLOW", 
 
     "property": "findById" 
 
    }, 
 
    { 
 
     "accessType": "READ", 
 
     "principalType": "ROLE", 
 
     "principalId": "$everyone", 
 
     "permission": "ALLOW", 
 
     "property": "count" 
 
    } 
 
    ], 
 
    "methods": {} 
 
}

商戶只有一個用戶 這是我的用戶模型

{ 
 
    "name": "UserData", 
 
    "base": "User", 
 
    "public": false, 
 
    "options": { 
 
    "mysql": { 
 
     "table": "tbl_users" 
 
    } 
 
    }, 
 
    "properties": { 
 
    "user_id": { 
 
     "type": "string", 
 
     "id": true, 
 
     "length": 36, 
 
     "defaultFn": "uuidv4", 
 
     "dataType": "char" 
 
    }, 
 
    "email": { 
 
     "type": "string" 
 
    } 
 
    }, 
 
    "validations": [], 
 
    "relations": {}, 
 
    "acls": [ 
 
    { 
 
     "accessType": "*", 
 
     "principalType": "ROLE", 
 
     "principalId": "$everyone", 
 
     "permission": "DENY" 
 
    }, 
 
    { 
 
     "accessType": "EXECUTE", 
 
     "principalType": "ROLE", 
 
     "principalId": "$everyone", 
 
     "permission": "ALLOW", 
 
     "property": "loginApp" 
 
    } 
 
    ], 
 
    "methods": {} 
 
}

我想創建一個遠程方法來添加商家和該商家的所有者。這是我目前的遠程方法:

'use strict'; 
 
var app = require('../../server/server'); 
 
module.exports = function(Obj) { 
 
\t Obj.createMerchant = function(req, cb) { 
 
\t \t //create the merchant 
 
\t \t Obj.beginTransaction({ 
 
\t \t \t isolationLevel: Obj.Transaction.READ_COMMITTED 
 
\t \t }, function(err, tx) { 
 
\t \t \t Obj.create([{ 
 
\t \t \t \t "merchant_name": req.merchant_name, 
 
\t \t \t \t "email": req.merchant_email 
 
\t \t \t }], {transaction: tx}, function(err, merchant){ 
 
\t \t \t \t if (err){ 
 
\t \t \t \t \t tx.rollback(); 
 
\t \t \t \t \t return cb(err, null); 
 
\t \t \t \t } else { 
 
\t \t \t \t \t Obj.prototype.user.create({ 
 
\t \t \t \t \t \t "merchant_id": merchant.merchant_id, 
 
\t \t \t \t \t \t "email": req.email, 
 
\t \t  \t \t \t "password": req.password 
 
\t \t \t \t \t }, {transaction: tx}, function(err, user) { 
 
\t \t \t \t \t \t if (err) { 
 
\t \t \t \t \t \t \t tx.rollback(); 
 
\t \t \t \t \t \t \t return cb(err, null); 
 
\t \t \t \t \t \t } else { 
 
\t \t \t \t \t \t \t tx.commit(function(err) { 
 
\t \t \t \t \t \t \t \t if (err){ 
 
\t \t \t \t  \t \t \t \t return cb(err, null); 
 
\t \t \t \t  \t \t \t } 
 

 
\t \t \t \t  \t \t \t return cb(null, merchant); 
 
\t \t \t \t \t   }); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t  }) 
 
\t \t \t \t } 
 
\t \t \t }) 
 
\t \t }); 
 
\t }; 
 

 
\t Obj.remoteMethod('createMerchant', { 
 
\t \t description: "Create merchant and it's owner", 
 
\t  accepts: [ 
 
\t   {arg: 'req', type: 'object', http: { source: 'body' }} 
 
\t  ], 
 
\t  returns: {arg: 'list', type: 'object'}, 
 
\t  http: {path:'/createMerchant', verb: 'post'} 
 
    \t }); 
 
};

遠程方法返回此錯誤:

{ 
    "error": { 
    "statusCode": 500, 
    "name": "Error", 
    "message": "HasOne relation cannot create more than one instance of UserData" 
    } 
} 

不知道如何解決這個問題?非常感謝

回答

0

無論如何,關係部分有一些邏輯錯誤。關係應該是「belongsTo」而不是「hasOne」,這是因爲hasOne會在父模型中創建或選擇id。我將結束這個問題。