我一直在享受v0.10中的新Sails關係,但是我目前面臨的最大挑戰是通過其關聯查找模型。如果我要填充一個手動關聯,比如一個ID數組,這將非常容易。但是,我似乎無法找到用Sails關聯來處理查找的正確方法。SailsJS v0.10由許多協會查找模型
我提供了一些示例代碼,概述兩個模型,公司和用戶。公司可以有很多用戶,而用戶只能有一個公司。這是一個非常直接的一對多關係,目標是找到與用戶ID匹配的所有公司。
## Company.js
name:
type: 'string'
required: true
users:
collection: 'User'
via: 'company'
## User.js
company:
model: 'Company'
required: true
last_name:
type: 'string'
required: true
first_name:
type: 'string'
required: true
## Lookup Users by Company ID of '2'
User.find(where: company: 2).exec(console.log)
# Result
# [] - Array of users matching that company ID
## ---- The Problem/Question ----
## Lookup Companies by User ID '1'
Company.find(where: users: contains: 1).exec(console.log)
# Result
# Error (E_UNKNOWN) :: Encountered an unexpected error:
# error: column company.users does not exist
# Details:
# { error: 'E_UNKNOWN',
# summary: 'Encountered an unexpected error',
# status: 500,
# raw: 'error: column company.users does not exist' }
我很感激任何關於處理這種查找的最佳方法的想法!
看起來ID值,用戶喜歡你的模型不正確定義: HTTP:// beta.sailsjs.org/#/documentation/reference/Models – Robin
它們是,它只是與屬性字面上不存在時一樣的消息。 – mbmufffin