2015-09-10 86 views
0

我使用的角度来构建我在后端和前端环回,我的模型有关系HasManyThrough如何删除属性之前保存

// person.json 
{ 
    "name": "Person", 
    ..., 
    "relations": { 
    "contacts": { 
     "type": "hasMany", 
     "model": "Person", 
     "foreignKey": "fromId", 
     "through": "PersonConnect" 
    } 
    } 
} 
// person-connect.json 
{ 
    "name": "PersonConnect", 
    "base": "PersistedModel", 
    "properties": ..., 
    "relations": { 
    "from": { 
     "type": "belongsTo", 
     "model": "Person", 
     "foreignKey": "fromId" 
    }, 
    "to": { 
     "type": "belongsTo", 
     "model": "Person", 
     "foreignKey": "toId" 
    } 
    } 
} 

如果我尝试用资源管理器,我可以建立一个新的关系使用

PUT /api/Person/:id/contacts/:fk 

两个人其中ID,该fromId和FK是风湿,棱角分明SDK产生的服务问题也发送车身参数ID和FK,这使得问题,因为设置PersonConnect.id之间的平等Person.fromId并附加额外的值fk

{ 
    "_id" : ObjectId("55f0915f19c46e06675d056e"), 
    ... 
    "fromId" : ObjectId("55f0915f19c46e06675d056e"), 
    "toId" : ObjectId("55f09b4d4d06f8c872e43c84"), 
    "fk" : "55f09b4d4d06f8c872e43c84" 
} 

固定我写了下面的

// person-connect.js 
var _ = require('lodash'); 

module.exports = function (PersonConnect) { 
    PersonConnect.observe('before save', function (ctx, next) { 
    if (ctx.instance) { 
     ctx.instance = _.omit(ctx.instance, ['id', 'fk']); 
    } 

    next(); 
    }); 
}; 

没有成功,该ID和FK值仍然使用的是发送的值,我设置为空,工作,但我得到的东西像

{ 
    "_id" : ObjectId("55f18c67bbfa11053b36cafc"), 
    ... 
    "fromId" : ObjectId("55f0915f19c46e06675d056e"), 
    "toId" : ObjectId("55f09b4d4d06f8c872e43c84"), 
    "fk" : null 
} 

如何在将模型存储在环回之前删除属性?

回答

1

您可以尝试使用unsetAttribute代替:

ctx.instance.unsetAttribute('unwantedField');