Loopback 3“hasOne”关系

时间:2017-06-12 06:44:46

标签: loopbackjs

我有2个模特

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) {
	Obj.createMerchant = function(req, cb) {
		//create the merchant
		Obj.beginTransaction({
			isolationLevel: Obj.Transaction.READ_COMMITTED
		}, function(err, tx) {
			Obj.create([{
				"merchant_name": req.merchant_name,
				"email": req.merchant_email
			}], {transaction: tx}, function(err, merchant){
				if (err){
					tx.rollback();
					return cb(err, null);
				} else {
					Obj.prototype.user.create({
						"merchant_id": merchant.merchant_id,
						"email": req.email,
		    			"password": req.password
					}, {transaction: tx}, function(err, user) {
						if (err) {
							tx.rollback();
							return cb(err, null);
						} else {
							tx.commit(function(err) {
								if (err){
				      				return cb(err, null);
				      			}

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

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

远程方法返回此错误:

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

知道如何解决这个问题吗?非常感谢

1 个答案:

答案 0 :(得分:1)

无论如何,关系部分存在一些逻辑错误。关系应该是“belongsTo”而不是“hasOne”,这是因为hasOne将在父模型中创建或选择id。我将结束这个问题。