Sails:删除所有元素并添加新的不能正常工作!! :(

时间:2014-11-07 19:43:14

标签: sails.js waterline

我尝试做一个简单的方法:删除收集的所有元素,然后插入新闻。

我有一个主要集合User,其集合Awards为外键:

award    : collection: 'Award'

Awards集合中,我有一个这样的方法:

updateAwards: (awards, cb) ->
      @award.remove award.id for award in @award
      @award.add award for award in awards
      @save(cb)

根本无法正常工作!我不知道是否是回调问题。

当我第一次调用方法时,集合为空并插入元素。如果为空并插入2个元素=>总共2个元素。没关系。

但是当我在另一个单独的请求中再次调用该函数时,插入另外两个元素:

[ mocha test ]
 expected: 2
 actual  : 4

最糟糕的是,如果我检查了localDiskDb文件,我有6个元素。笏?

"user": [
      {
        "email": "user1@sailor.com",
        "username": "user1",
        "picture": "assets/images/default.png",
        "rol": "user",
        "label": "Kiko Beats",
        "online": true,
        "summary": "Hello, this is my public profile and this is a generic summary information.",
        "country": "Internet, Worldwide.",
        "createdAt": "2014-11-07T19:25:29.077Z",
        "updatedAt": "2014-11-07T19:25:29.875Z",
        "id": 1
      }
    ],
    "award": [
      {
        "title": "IV Spanish Championship",
        "year": "2010",
        "description": "national event",
        "createdAt": "2014-11-07T19:25:29.666Z",
        "updatedAt": "2014-11-07T19:25:29.666Z",
        "id": 1
      },
      {
        "title": "Zombeats Summer Battle",
        "year": "2011",
        "description": "the best event of the summer",
        "createdAt": "2014-11-07T19:25:29.668Z",
        "updatedAt": "2014-11-07T19:25:29.668Z",
        "id": 2
      },
      {
        "description": "national event",
        "title": "IV Spanish Championship",
        "year": "2010",
        "createdAt": "2014-11-07T19:25:29.880Z",
        "updatedAt": "2014-11-07T19:25:29.880Z",
        "id": 3
      },
      {
        "description": "the best event of the summer",
        "title": "Zombeats Summer Battle",
        "year": "2011",
        "createdAt": "2014-11-07T19:25:29.881Z",
        "updatedAt": "2014-11-07T19:25:29.881Z",
        "id": 4
      },
      {
        "title": "IV Spanish Championship",
        "year": "2010",
        "description": "national event",
        "createdAt": "2014-11-07T19:25:29.900Z",
        "updatedAt": "2014-11-07T19:25:29.900Z",
        "id": 5
      },
      {
        "title": "Zombeats Summer Battle",
        "year": "2011",
        "description": "the best event of the summer",
        "createdAt": "2014-11-07T19:25:29.902Z",
        "updatedAt": "2014-11-07T19:25:29.902Z",
        "id": 6
      }
    ]

问题是...... 为什么?为了正常工作需要做些什么?

更新个人资料的主要用户方法:

async.waterfall [
          (cb) ->
            User.update(user.id, user).exec (err, user) -> cb err, user
          (user, cb) ->
            # because update doesnt support populate...
            User.findOne(user.id).populate('award').populate('contact').exec (err, user) -> cb err, user
          (user, cb) ->
            user.updateAwards award, (err) -> cb err
          (cb) ->
            # ensure that recover the last version of the user....
            User.findOne(user.id).populate('award').populate('contact').exec (err, user) -> cb err, user
        ], (err, user) ->
          return res.negotiate(err)  if err
          user = params.user unless user?
          res.ok([user, user.award])

如果你需要检查代码的任何部分,那么所有内容都在Github中:

controller => https://github.com/Zombeats/backend/blob/master/api/controllers/ProfileController.coffee award => https://github.com/Zombeats/backend/blob/master/api/models/Award.coffee

0 个答案:

没有答案