2016-12-05 28 views
0

我有兩種模式:Users和InstalledApps。模型是這樣的:sails js多對多記錄插入錯誤

//Users Model 
attributes:{ 
name: 'string', 
age: 'string', 
installedApps:{ 
    collection: 'installedApps', 
    via: 'users' 
} 
} 

我InstalledApps模式是這樣的:

attributes:{ 
deviceID : 'string', 
users:{ 
    collection:'users', 
    via:'installedApps' 
} 
} 

現在我已經創建的用戶ID爲1和2 但是當我通過像郵遞員InstalledApps插入數據這個:

{ 
"users": [1,2], 
"deviceID": "123456", 
} 

它彈出一個錯誤:Unknown rule: default。我不知道我錯在哪裏?

回答

0

我想你讓你的模型錯了。 您應該使用http://sailsjs.com/documentation/concepts/models-and-orm/associations/through-associations。 然後你應該有3個模型。 USER,APP,INSTALLED_APPS。

您的用戶模型將得到:

//Users Model 
attributes:{ 
name: 'string', 
age: 'string', 
installedApps:{ 
    collection: 'APP', 
    via: 'user', 
through: 'installedApps' 
} 
} 

和你的APP模式:

//APP Model 
attributes:{ 
device: 'string', 
installedApps:{ 
    collection: 'APP', 
    via: 'app', 
through: 'installedApps' 
} 
} 

最後是你的INSTALLED_APPS:

//INSTALLED_APPS Model 
    attributes:{ 
    user: { 
     model: 'user' 
    }, 
    app: { 
     model: 'app' 
    } 
    } 

然後你只需要使用簡單的創建安裝每個用戶/應用或藍圖的APP http://sailsjs.com/documentation/reference/blueprint-api/add-to

希望它有幫助。

1

您的模型很好。你確定你將數據發佈爲JSON嗎?在Postman的Body選項卡中,選擇raw,然後從Text更改爲JSON (application/json)。在"123456"之後刪除多餘的逗號,並且您的請求應該沒有問題。另外,請確保您正在對/installedApps路線進行POST請求,例如, http://localhost:1337/installedApps

我使用您在新版Sails.js應用中發佈的模型定義對此進行了測試,獲取的installedApps條目正確插入並關聯到users條目。

+0

實際上我在json中發送'autoUpdateAt',這是導致錯誤。我認爲'autoUpdatedAt'不應該被髮送。 –

+0

@DevendraVerma沒錯 - 'createdAt'和'updatedAt'是默認自動處理的(可在'config/models.js'中全局配置或者在模型文件中爲特定的模型配置)。很高興你解決了這個問題。 =) – Viktor