问题描述
这里的错误:
我的用户模型的主键是用户名。我的路线的主键是routename。我的API返回内部的数据jsons:{}按照jsonapi.org规格。从而id属性是不是在顶层,如JS-数据的需求。这就是为什么我在一个afterFind返回数据。数据为用户。我试图做这样的事情在路线,但它的路由数组。
控制台登录beforeInject给我:
这里的配置:
DS.defineResource({
名称:'用户',
idAttribute:用户名,
基本路径:apiEndpoint, 关系:{
有很多: {
路线:{
localField:'路线',
FOREIGNKEY:用户名
}
}
},
//仅有此资源
一个afterFind:功能(资源,数据,CB){
//做更具体的东西,用户
CB(NULL,数据。数据);
}
});DS.defineResource({
名称:'路线',
idAttribute:routename',
基本路径:apiEndpoint,
cacheResponse:真实,
关系:{
属于: {
用户:{
家长:真,
localKey:用户名,
localField:'用户'
}
}
},
beforeInject:功能(资源,数据){
//做更具体的东西,用户
的console.log(数据);
返回data.data.routes;
}
});
这里就是我尝试加载我的路线,但得到错误:
解析:{
用户:函数($路径,DS){
变种的用户名= $ route.current.params.username;
返回DS.find(用户,用户名)。然后(功能(用户){
DS.loadRelations(用户,user.username,['路线']),然后(功能(用户){
的console.log(用户);
},功能(错误){
的console.log(ERR);
});
});
}
}
不仅是下了数据字段中嵌套的数据,而是一个路径字段。所以,当你找到路线,你想注入是这样的:
{
路线:[{
// FOREIGNKEY给用户
用户名:john1337',
路线的//主键
编号:1234
}]
}
当你需要注入:
[{
用户名:john1337',
ID:1
}]
一个 afterFindAll
在你的路由受资源
添加到 CB(NULL,data.data.routes)
。
你要么需要:
A)加入大量的钩所有的资源或后
b)作出反序列化通用的,所以它适用于所有资源。也许这样的事情?
DS.defaults.afterFind =功能(资源,数据,CB){
CB(NULL,数据。数据[Resource.name])
};
DS.defaults.afterFindAll =功能(资源,数据,CB){
CB(NULL,数据。数据[Resource.name])
};
Here's the error:http://puu.sh/lXzja/f773fb6c9a.png
The primary key for my user model is username. The primary key for my routes is the routename. My api returns jsons inside data:{} as per jsonapi.org specs. Thus the id attribute is not in the top-level, as js-data demands. This is why I return data.data in the afterFind for 'users'. I tried to do something like that in 'routes' but it's an array of routes. The console log in beforeInject gives me:
Here's the config:
DS.defineResource({
name: 'users',
idAttribute: 'username',
basePath: apiEndpoint,
relations: {
hasMany: {
routes: {
localField: 'routes',
foreignKey: 'username'
}
}
},
// set just for this resource
afterFind: function(resource, data, cb) {
// do something more specific to "users"
cb(null, data.data);
}
});
DS.defineResource({
name: 'routes',
idAttribute: 'routename',
basePath: apiEndpoint,
cacheResponse: true,
relations: {
belongsTo: {
users: {
parent: true,
localKey: 'username',
localField: 'users'
}
}
},
beforeInject: function(resource, data) {
// do something more specific to "users"
console.log(data);
return data.data.routes;
}
});
Here's where I try to load my routes but get that err:
resolve: {
user: function($route, DS) {
var username = $route.current.params.username;
return DS.find('users', username).then(function(user) {
DS.loadRelations('users', user.username, ['routes']).then(function(user) {
console.log(user);
}, function(err) {
console.log(err);
});
});
}
}
Not only is your data nested under a "data" field, but a "routes" field. So when you find the routes, you're trying to inject something like:
{
routes: [{
// foreignKey to a user
username: 'john1337',
// primary key of a route
id: 1234
}]
}
when you need to be injecting:
[{
username: 'john1337',
id: 1
}]
Add an afterFindAll
on your routes resource to cb(null, data.data.routes)
.
You'll either need to:
A) Add lots of "after" hooks to all your Resources orB) Make the deserialization generic so it works for all Resources. Perhaps something like this?
DS.defaults.afterFind = function (Resource, data, cb) {
cb(null, data.data[Resource.name])
};
DS.defaults.afterFindAll = function (Resource, data, cb) {
cb(null, data.data[Resource.name])
};
这篇关于JS-数据错误:ATTRS必须包含idAttribute指定的属性 - 使用的hasMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!