【问题标题】:Ember get data from MongoDB with NodeJSEmber 使用 NodeJS 从 MongoDB 获取数据
【发布时间】:2018-04-23 12:26:54
【问题描述】:

我有一个 MongoDB 服务器,它有一个 NodeJS 服务器与之交互。我希望 Ember Store 从此服务器获取数据。一个教程引导我直到这一点,但它对我不起作用(tutorial),我认为它已经过时但我不确定。 后端服务器这部分是关于发送数据的:

app.get('/api/appointments', function(req, res){
    AppointmentModel.find({}, function(err, docs){
        if(err) res.send({error:err});
        else res.send({data:docs, "type":"appointment"});
    });
});

一个 curl 请求证明它确实有效:

curl http://localhost:4500/api/appointments
{"data":[{"flags":[],"dates":[],"invited":[],"_id":"5adc8b6724a69f4327c4c7af","author":7,"title":"Does this work"}],"type":"appointment"}

这是向服务器发出请求的 ember 代码部分:

export default Route.extend({
    model: function(){
        return this.store.findAll('appointment');
    }
});

这是我在 webbrowser(chrome) 的控制台中遇到的错误:

Error while processing route: index Assertion Failed: Encountered a resource object with an undefined type (resolved resource using (unknown mixin)) Error: Assertion Failed: Encountered a resource object with an undefined type (resolved resource using (unknown mixin))
    at new EmberError (http://localhost:4200/assets/vendor.js:24475:25)
    at Object.assert (http://localhost:4200/assets/vendor.js:24725:15)
    at Class._normalizeResourceHelper (http://localhost:4200/assets/vendor.js:85052:61)
    at Class._normalizeDocumentHelper (http://localhost:4200/assets/vendor.js:84993:25)
    at Class._normalizeResponse (http://localhost:4200/assets/vendor.js:85121:36)
    at Class.normalizeArrayResponse (http://localhost:4200/assets/vendor.js:86013:19)
    at Class.normalizeFindAllResponse (http://localhost:4200/assets/vendor.js:85873:19)
    at Class.normalizeResponse (http://localhost:4200/assets/vendor.js:85816:23)
    at normalizeResponseHelper (http://localhost:4200/assets/vendor.js:76037:39)
    at promise.then.adapterPayload (http://localhost:4200/assets/vendor.js:76889:19)

因为错误与类型有关,所以我检查并再次检查了模型,但没有发现任何会出错的东西。 余烬:

export default DS.Model.extend({
    author: DS.attr('number'),
    title: DS.attr('string'),
    description: DS.attr('string'),
    flags: DS.hasMany('number'),
    dates: DS.hasMany('date'),
    invited: DS.hasMany('number')
});

NodeJS/Mongo:

//The schema for appointments
var appointmentSchema = new mongoose.Schema({
    author: Number, //Userid of the owner of the appointment.
    title: String,
    description: String,
    flags: [Number], //Array of ids of flags that are set.
    dates: [Date],
    invited: [Number] //Array of user ids.
});
var AppointmentModel = mongoose.model('appointment', appointmentSchema);

ember 需要以什么特殊方式处理数据?

我已经阅读了一些关于 store 和 ember 的文档,但我对框架还是新手,非常感谢任何帮助。

【问题讨论】:

  • 调试提示:“return this.store.findAll('appointment');”而不是这个尝试“返回{“作者”:“约翰”}”。您可以尝试各种 JSON 进行调试。

标签: node.js mongodb ember.js ember-data


【解决方案1】:

首先您需要了解 ember 数据模型定义。这没有意义:

flags: DS.hasMany('number'),

这基本上意味着*一个约会有多个标志,它们的模型是number。您不能将hasMany 或belongsTo 用于数组或原始类型。

现在您有两个选择。您可以为标志、人员(受邀)等创建模型,或者返回简单的 JSON 结构。为此指定attr()(未给出参数)。

您的下一个问题是默认情况下 ember 假定 jsonapi,但您没有发送 jsonapi。在这里,您可以移动到服务器上的jsonapi,或者修改您的serializer,如here。


但是考虑到所有这些,我得出的结论是,您可能根本不想使用ember-data。 ember-data 对于具有关系且基于模型的数据非常有用,但您可以在没有它的情况下使用 ember。

你可以这样做:

model() {
  return $.getJSON('/api/appointments');
}

然后对您发送的纯 JSON 进行操作。

是否要使用 ember-data 完全取决于您的用例。

【讨论】:

【解决方案2】:

据我所知,您使用了错误的适配器,您的后端使用的是 JSONAPI 规范 http://jsonapi.org/,但 ember 需要 REST,这就是 type 属性失败的原因,您只需从 REST 更改适配器到 JSONAPI,在这里您可以找到更多信息 https://guides.emberjs.com/v3.0.0/models/customizing-adapters/ 在您的情况下应该非常简单,只需将适配器更改为 JSONAPIAdapter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2021-12-19
    • 2019-12-27
    相关资源
    最近更新 更多