问题:
我使用rest客户端通过我的Rails后端从newsapi.org中提取新闻文章。我可以在localhost上显示这些数据:3000/文章。我不想(必然)将这些数据保存到数据库中,只想在我的Rails后端调用它之后,将它显示在我的Ember前端。我知道这个错误:
Error while processing route: article Assertion Failed: You must include an 'id' for article in an object passed to 'push'
我理解这意味着我的数据没有“id”--它没有。我无法控制输入的数据。
{
"status": "ok",
"totalResults": 6868,
"articles": [
{
"source": {
"id": "mashable",
"name": "Mashable"
},
"author": "Lacey Smith",
"title": "Cannabis may alter the genetic makeup of sperm",
"description": "A new study suggests that marijuana use can not
only lower sperm count, but also affect sperm DNA. Read more... More
about Marijuana, Cannabis, Sperm, Science, and Health",
"url": "https://mashable.com/video/cannabis-sperm-dna/",
"content": null
},
成员-数据不喜欢这样,因为它不是JSONAPI格式,我不能将它更改为JSONAPI。如何在不使用ember -数据和不必将其保存到我的数据库的情况下,在余烬前端显示这些数据?我发现的教程都有使用数据库和Ember数据的例子。
我已经尝试过的:
我试着把它保存到我的数据库并从那里加载,但我认为这可能会使这个想法复杂化,因为我需要刮掉数据并将其保存到我的数据库中,事实证明这很难做到。我对后端的东西没有经验,也没有找到多少答案。如果你有这样做的答案,可以在这里回答:
我愿意尝试一下。我原以为这是最好的办法。我可以保存数据,只在返回新文章时添加到数据库中。我没有运气,所以我想我应该从另一个方向接近它。
谢谢你的帮助!
发布于 2019-01-28 01:28:49
如果您想在ember-data
之外执行一些处理,请创建一个服务。在您的服务中,您需要向后端发出ajax/fetch请求。假设您正在使用余烬-ajax来发出请求:
import Service, { inject } from '@ember/service';
export default Service.extend({
// inject the ember-ajax ajax service into your service
ajax: inject(),
//this returns a promise
getNewsItems(){
var options = {
// request options (aka anything needed to authenticate against your api, etc)
};
let url = "yourBackendEndpoint/for/this/resource";
return request(url, options).then(response => {
// `response` is the data from the server
// here you want to convert the JSON into something useable
return response;
});
}
});
然后,假设在某个路由的模型钩子中,需要这个服务(我们将在your-app/services/my-service.js
中定义该服务)来获取新闻提要条目:
import Route from '@ember/route';
import { inject } from '@ember/service';
export default Route.extend({
myService: inject(),
model(){
let myService = this.get('myService');
return myService.getNewsItems();
}
)
我个人使用余烬-取,它避免了jQuery的依赖,并且可以在我的应用程序的所有目标浏览器中使用。我不使用烬-数据在我的任何应用程序。
https://stackoverflow.com/questions/54395159
复制相似问题