2013-11-05 64 views
4
attributes: { 
    username: { 
     type: 'email', // validated by the ORM 
     required: true 
    }, 
    password: { 
     type: 'string', 
     required: true 
    }, 
    profile: { 
     firstname: 'string', 
     lastname: 'string', 
     photo: 'string', 
     birthdate: 'date', 
     zipcode: 'integer' 
    }, 
    followers: 'array', 
    followees: 'array', 
    blocked: 'array' 
} 

我目前註冊用戶,然後更新配置文件信息後註冊。如何開始將配置文件數據添加到此模型?Sails.js - 如何更新嵌套模型

我在其他地方看過這個推送方法應該可以工作,但事實並非如此。我得到這個錯誤:類型錯誤:對象的翻譯:有沒有方法「推」

 Users.findOne(req.session.user.id).done(function(error, user) { 

      user.profile.push({ 
       firstname : first, 
       lastname : last, 
       zipcode: zip 
      }) 

      user.save(function(error) { 
       console.log(error) 
      }); 

     }); 
+0

您正在使用哪個數據庫? – colbydauph

+0

我正在使用MongoDB – diskodave

回答

4

@Zolmeister是正確的。帆僅支持以下模型屬性類型

string, text, integer, float, date, time, datetime, boolean, binary, array, json

他們也並不支持協會(否則在這種情況下很有用)

GitHub Issue #124

您可以通過繞過帆和使用像這樣的蒙戈的本地方法解決這個問題:

Model.native(function(err, collection){ 

    // Handle Errors 

    collection.find({'query': 'here'}).done(function(error, docs) { 

     // Handle Errors 

     // Do mongo-y things to your docs here 

    }); 

}); 

請記住,他們的墊片有原因的。繞過它們會刪除一些在幕後處理的功能(將id查詢轉換爲ObjectIds,通過套接字發送pubsub消息等)。

+0

不確定這是否有效 - 'collection.find'沒有promise風格的'done'方法。 –

+0

它可能已在v0.10中更改。如果是這樣的話,它可能仍然遵循作爲.find()的最後一個參數傳遞迴調的標準。 – colbydauph

2

目前帆不支持嵌套模型定義(據我所知)。您可以嘗試使用'json'類型。 之後,你只會有:

user.profile = { 
    firstname : first, 
    lastname : last, 
    zipcode: zip 
}) 

user.save(function(error) { 
    console.log(error) 
}); 
+4

這是令人失望的是Sails是爲mongoDB構建的......這是使用面向文檔的數據庫的優勢。 – diskodave

1

太遲不能回覆,但對於其他人(作爲參考),他們可以這樣做:

Users.findOne(req.session.user.id).done(function(error, user) { 
    profile = { 
      firstname : first, 
      lastname : last, 
      zipcode: zip 
     }; 
    User.update({ id: req.session.user.id }, { profile: profile},   
     function(err, resUser) { 
    });   
});