简体   繁体   English

保存记录以设置id ember-data时服务器的响应

[英]Response from the server when saving a record to set the id ember-data

After I save the record using selection.save() where selection is my model, id is not being set on the model. 在使用selection.save()保存记录后,其中selection是我的模型,未在模型上设置id。 My current server response is same as the json for show. 我当前的服务器响应与show的json相同。 ie: selection: {id: <id>} and other fields. 即: selection: {id: <id>}和其他字段。 Buy id is not set in the ember data local storage. 购买ID未在ember数据本地存储中设置。 I'm using the default RESTAdapter and Rails as backend. 我使用默认的RESTAdapter和Rails作为后端。 (I use jbuilder for JSON) (我使用jbuilder for JSON)

model is MatchPlayerSelection and once I save the record, this is the response I send back: model是MatchPlayerSelection ,一旦我保存记录,这就是我发回的响应:

{"match_player_selection":{"id":41298,"user":3,"scheduledMatch":133,"matchPlayerScores":[3697,3696,3694,3698,3704,3719,3712,3709,3717,3707,3714],"captain":3694,"viceCaptain":3709}

How are you testing for the id getting set? 你是如何测试id设置的? The save function is async and returns a promise, so you need to resolve the promise before checking the id . save函数是异步并返回一个promise,因此您需要在检查id之前解析promise。

This will NOT work: 这不起作用:

selection.save();
console.log( selection.get('id') );

This WILL work: 这将工作:

selection.save().then(function(savedSelection){
  console.log( savedSelection.get('id') );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM