Ember:断言失败:您必须在传递给'push'的对象中包含服务器帐户的'id'

时间:2017-02-21 07:34:16

标签: rest ember.js

我收到了一个奇怪的错误。我输入一个表单,它正确地将数据作为对象输出并通过REST发送。但是每当我这样做时,我仍然会收到此错误。我已将primaryKey更改为“_id”。我试过“用户名”,但它给了我一个完全不同的错误,没用。

<div class="js-recent-item recent-item" data-view-url="{{url}}">
  <label class="report-card-title" title="{{title}}">
    <span>title 1 </span>
  </label>
  <div class="no-sides-padding report-container">
    <div class="report-card-text" title="{{description}}">
      description 1 
    </div>
  </div>
</div>

<div class="js-recent-item recent-item" data-view-url="{{url}}">
  <label class="report-card-title" title="{{title}}">
    <span>first title title title title title title title title title last </span>
  </label>
  <div class="no-sides-padding report-container">
    <div class="report-card-text Aligner" title="{{description}}">
      first description description description description description description description description description description description description description description description last
    </div>
  </div>
</div>

AccountAdapter.js

Object {username: "adas", password: "dasdsa", pincode: "dasdasdsadsad", centcoin: "0"}
ember.debug.js:19160 Error: Assertion Failed: You must include an 'id' for account in an object passed to 'push'
    at EmberError (ember.debug.js:19083)
    at assert (ember.debug.js:6778)
    at Object.assert (ember.debug.js:18895)
    at assert (ember-data.js:646)
    at Class._pushInternalModel (ember-data.js:9725)
    at Class.push (ember-data.js:9701)
    at ember-data.js:10079
    at Backburner.run (ember.debug.js:294)
    at Class._adapterRun (ember-data.js:9950)
    at ember-data.js:10074
defaultDispatch @ ember.debug.js:19160
dispatchError @ ember.debug.js:19143
onerrorDefault @ ember.debug.js:32916
trigger @ ember.debug.js:51624
(anonymous) @ ember.debug.js:52618
invoke @ ember.debug.js:990
flush @ ember.debug.js:1054
flush @ ember.debug.js:862
end @ ember.debug.js:176
run @ ember.debug.js:298
join @ ember.debug.js:318
run.join @ ember.debug.js:23770
hash.success @ ember-data.js:12808
fire @ jquery.js:3305
fireWith @ jquery.js:3435
done @ jquery.js:9242
(anonymous) @ jquery.js:9484

AccountSerializer.js

createRecord(store, type, snapshot) {
            var data = {};
            var serializer = store.serializerFor(type.modelName);
            var url = this.buildURL(type, null, snapshot, 'createRecord');

            //serializer.serializeIntoHash(data, null, snapshot, { includeId: false });
            data = this.serialize(snapshot, {includeId: false});
            console.log(data);
            return this.ajax(url, "POST", { data: data });
        },
        urlForCreateRecord( type ) {
            return this._buildURL( type);
        },

AccountsModel.js

import AccountsSerializer from "store/ServerSerializer";


export default AccountsSerializer.extend({
    primaryKey: "_id",

    modelNameFromPayloadKey() {
        return "serverAccount";
    },

});

AccountsNewRoute.js

import { attr,
    Model } from "EmberData";


export default Model.extend({
    username : attr( "string" ),
    password: attr( "string" ),
    pincode  : attr( "string" ),
    centcoin  : attr( "string" ),

}).reopenClass({
    toString() { return "accounts"; }
});

ServerSerializer.js

save() {
            const store = get( this, "store" );

            var model = get(this, "controller.model");
            store.createRecord("serverAccount", {
                //id: get(model, "username"),
                username: get(model, "username"),
                password: get(model, "password"),
                pincode: get(model, "pincode"),
                centcoin: "0"
            }).save().then((saved) => {
                console.log("THIS IS SAVED ADD ACCOUNT", get(saved, "pincode"));
                this.send("addAccount", saved);
                return saved;
            });
            //let account = this.controller.get("model");
            //let account = get(this, "controller.model");
            console.log("THIS IS SAVED ADD ACCOUNT", get(model, "username"));
            console.log("THIS IS SAVED ADD ACCOUNT", get(model, "password"));
            console.log("THIS IS SAVED ADD ACCOUNT", get(model, "pincode"));


        },

服务器应该回复:

import {
    RESTSerializer,
} from "EmberData";
export default RESTSerializer.extend({
    isNewSerializerAPI: true,

    primaryKey: "_id",

});

1 个答案:

答案 0 :(得分:1)

您的问题是您正在使用RESTSerializer,这需要将有效负载包装到另一个对象中,例如: G:

{
  "account": {
    "username": "dadsdsadsa",
    "pincode": "fdsfdsfd",
    "password": "fdsfdsfdsfds",
    "createdAt": "2017-02-21T07:33:53.870Z",
    "setUpdatedAt": "2017-02-21T07:33:53.870Z",
    "_id": "twY0MFUeNf4UP7XO"
  }
}

请尝试使用JSONSerializer