【问题标题】:mongoose & ember, record rejected reason: typeerrormongoose & ember,记录被拒绝原因:typeerror
【发布时间】:2013-11-29 00:31:20
【问题描述】:

我在回复中收到此类型错误,但我不知道 store.find('someThread') 期望什么格式。 这是我的余烬部分:

App.RestAdapter = DS.RESTAdapter.extend({
  url: 'http://mylocalhost:3000',
  namespace: 'api',

  serializer: DS.RESTSerializer.extend({
    primaryKey: function(type) {
        return '_id';
    }
  })
});

App.Store = DS.Store.extend({
  revision: 12,
  adapter: App.RestAdapter
});

App.VideoRoute = Ember.Route.extend({
  model: function() {
    console.log("video..", this.store.find('video'));
    return true;
  }
});

这是节点部分:

exports.list = function(req, res) {
  Video.find(function(err, videos) {
    var payload = { video: {name: "sampleName"}};
    return res.send(200, JSON.stringify(payload));
  });
}

这是响应的 console.log:

Class {constructor: function, reason: null, isPending: undefined, isSettled: undefined, isRejected: false…}
  __ember1385638132476: undefined
  __ember1385638132476_meta: Meta
  _super: undefined
  arrangedContent: (...)
  content: (...)
  isRejected: true
  reason: TypeError
  message: "Object function () {↵    if (!wasApplied) {↵  (...)"
  stack: (...)
  get stack: function () { [native code] }
  set stack: function () { [native code] }
  __proto__: Error
  __proto__: Object

如果我只发送一个字符串,我确实会收到响应:`res.send("something"); 但这只是在responseText中,isRejected是真的,所以...

有什么想法吗?

更新

*这是截断的消息部分:*

"Object function () {
if (!wasApplied) {
  Class.proto(); // prepare prototype...
}
o_defineProperty(this, GUID_KEY, undefinedDescriptor);
o_defineProperty(this, '_super', undefinedDescriptor);
var m = meta(this), proto = m.proto;
m.proto = this;
if (initMixins) {
  // capture locally so we can clear the closed over variable
  var mixins = initMixins;
  initMixins = null;
  this.reopen.apply(this, mixins);
}
if (initProperties) {
  // capture locally so we can clear the closed over variable
  var props = initProperties;
  initProperties = null;

  var concatenatedProperties = this.concatenatedProperties;

  for (var i = 0, l = props.length; i < l; i++) {
    var properties = props[i];

    Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));

    if (properties === null || typeof properties !== 'object') {
      Ember.assert("Ember.Object.create only accepts objects.");
      continue;
    }

    var keyNames = Ember.keys(properties);
    for (var j = 0, ll = keyNames.length; j < ll; j++) {
      var keyName = keyNames[j];
      if (!properties.hasOwnProperty(keyName)) { continue; }

      var value = properties[keyName],
          IS_BINDING = Ember.IS_BINDING;

      if (IS_BINDING.test(keyName)) {
        var bindings = m.bindings;
        if (!bindings) {
          bindings = m.bindings = {};
        } else if (!m.hasOwnProperty('bindings')) {
          bindings = m.bindings = o_create(m.bindings);
        }
        bindings[keyName] = value;
      }

      var desc = m.descs[keyName];

      Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
      Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
      Ember.assert("`actions` must be provided at extend time, not at create time, when Ember.ActionHandler is used (i.e. views, controllers & routes).", !((keyName === 'actions') && Ember.ActionHandler.detect(this)));

      if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
        var baseValue = this[keyName];

        if (baseValue) {
          if ('function' === typeof baseValue.concat) {
            value = baseValue.concat(value);
          } else {
            value = Ember.makeArray(baseValue).concat(value);
          }
        } else {
          value = Ember.makeArray(value);
        }
      }

      if (desc) {
        desc.set(this, keyName, value);
      } else {
        if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
          this.setUnknownProperty(keyName, value);
        } else if (MANDATORY_SETTER) {
          Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
        } else {
          this[keyName] = value;
        }
      }
    }
  }
}
finishPartial(this, m);
this.init.apply(this, arguments);
m.proto = proto;
finishChains(this);
sendEvent(this, "init");
} has no method 'extract'"

更新 2:

发现您可以覆盖 ajax 方法。现在,至少我知道,json 是在成功函数上传输的,但数据属性仍然未定义......

App.ApplicationAdapter = DS.RESTAdapter.extend({
  url: 'http://localhost:3000',
  namespace: 'api',

  ajax: function(url, type, hash) {
   var adapter;
   adapter = this;
   return new Ember.RSVP.Promise(function(resolve, reject) {
    var headers;
    hash = hash || {};
    hash.url = url;
    hash.type = type;
    hash.dataType = "json";
    hash.context = adapter;
    if (hash.data && type == "GET") {
      hash.contentType = "application/json; charset=utf-8";
      hash.data = JSON.stringify(hash.data);
    }
    if (adapter.headers !== undefined) {
      headers = adapter.headers;
      hash.beforeSend = function(xhr) {
        return forEach.call(Ember.keys(headers), function(key) {
          return xhr.setRequestHeader(key, headers[key]);
        });
      };
    }
    hash.success = function(json) {
      console.log("success", hash.data, json); //-> undefined, Object {video: Object}
      return Ember.run(null, resolve, json);
    };
    hash.error = function(jqXHR, textStatus, errorThrown) {
      return Ember.run(null, reject, adapter.ajaxError(jqXHR));
    };
    return Ember.$.ajax(hash);
  });
 },
});

【问题讨论】:

  • 您能否显示出现在message 属性中的内容。您的堆栈跟踪显示它可能会截断消息显示"Object function () {↵ if (!wasApplied) {↵ ... doesn't have method foo" 等。这可能会有所帮助。
  • 谢谢@MárcioRodriguesCorreaJúnior,更新了帖子。我不得不说,我是这个后端的新手。我通常在没有 ember-data 的情况下工作。
  • 你的 ember-data 版本是多少?
  • ember 是 1.2.0,ember-data 是 1.0.0 beta 3

标签: javascript ember.js mongoose ember-data


【解决方案1】:

如果你使用的是 ember-data 1.0.0-beta.x 版本,你需要像下面这样声明你的适配器和序列化器:

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
  url: 'http://mylocalhost:3000',
  namespace: 'api'
});

App.VideoRoute = Ember.Route.extend({
  model: function() {
    console.log("video..", this.store.find('video'));
    return true;
  }
});

有一个转换指南here,它告诉我们 0.13 和 1.0.0-beta.x 版本之间的变化是什么

【讨论】:

  • 你的 ember-data 版本是多少?
  • 你的问题是因为没有正确创建序列化程序,而不是serializerInstance.extract,你的设置是在做App.MySerializer.extract,一个类没有这个方法。
  • 找到了解决方案:我只是忘了在 json-payload 周围加上方括号......不知何故必须有括号...... var payload = { video: [{_id: 1 ,名称:“theName”,标题:“theTitle”}] };返回 res.send(JSON.stringify(payload));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2011-01-20
  • 2015-03-29
  • 1970-01-01
相关资源
最近更新 更多