Open coolneka opened 8 years ago
Animal.find(...).eager('owner').all(...)
seems .eager('owner')
dosen't work , I still get :
[{id:1 , person_id:1 ,name:dog }, {id:2 , person_id:2 ,name:cat }]
pet.owner.name
?
yeah , is there some convenience method for to do this
I suspect the problem is that .owner
is not enumerable and so a basic JSON.stringify
won't work.
You may need to map:
_.map(items, function (i) { return _.extend({}, i, { owner: i.owner }) })
..although it should be enumerable :-\
problem unsolved; this is my topic Model:
module.exports = function (orm, db) {
var ti_topic = db.define('ti_topic', {
id : { type: 'text', required: true , key:true},
user_id : { type: 'text' },
topic_name : { type: 'text' },
topic_cotent : { type: 'text' },
support_frequency : { type: 'integer' },
reply_frequency : { type: 'integer' },
views : { type: 'integer' },
in_date : { type: 'date', time: true },
up_date : { type: 'date', time: true }
},
{
hooks: {
beforeValidation: function () {
if(!this.id){
this.id = randomGen.alphaNum(15);
}
if(!this.in_date){
this.in_date = new Date();
}
this.up_date = new Date();
}
},
});
ti_topic.hasOne('user' ,db.models.ti_user ,{field:'user_id' ,reverse:'topics'})
};
this is my topic-data-interface
list : function(req, res, next){
var result = {};
var datas = new Array();
req.models.ti_topic.find({}).eager('user').all(function(err ,topics){
if(err) return next(err);
var items = _.map(topics ,function(i){
var data = _.extend({} ,i ,{user : i.user})
return data;
})
result.data = items;
res.json(result)
}).count(function(err ,count){
result.count = count;
})
}
Still ,I can't get my user info
I've tried to use the getOwner Method in a foreach circle structure :
list : function(req, res, next){
var result = {};
var datas = new Array();
req.models.ti_topic.find().all(function(err ,topics){
if(err) return next(err);
var i = 0 ;
topics.forEach(function(topic){
topic.getUser(function(err ,user){
i++
if(err) return next(err);
datas.push(user)
if(i === topics.length){
result.data = datas;
res.json(result)
}
})
})
}).count(function(err ,count){
result.count = count;
})
}
but i get an error:
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at ServerResponse.json (E:\开发\node\node_modules\express\lib\response.js:24
2:19)
at E:\开发\node\app\controllers\topic_controller.js:99:29
at saveAndReturn (E:\开发\node\node_modules\node-orm\lib\Associations\One.js
:173:12)
at Object.exports.get (E:\开发\node\node_modules\node-orm\lib\Singleton.js:2
3:11)
at E:\开发\node\node_modules\node-orm\lib\Model.js:299:14
at Query._callback (E:\开发\node\node_modules\node-orm\lib\Drivers\DML\mysql
.js:226:11)
at Query.Sequence.end (E:\开发\node\node_modules\mysql\lib\protocol\sequence
s\Sequence.js:96:24)
at Query._handleFinalResultPacket (E:\开发\node\node_modules\mysql\lib\proto
col\sequences\Query.js:144:8)
at Query.EofPacket (E:\开发\node\node_modules\mysql\lib\protocol\sequences\Q
uery.js:128:8)
now , I am trying to do this with Raw queries
Maybe ,we should add support to Database View..
@dxg seems problem solved , when i have
Animal.hasOne('owner', Person ,{autoFetch:true}).
I can get owner info withAnimal.find(…)
but it cause another problem:
how do i use Animal.find(…) with a result not contain its owner info
Is there an option for us to decide whether or not Animal.find(…)
contains its owner info
Eg:
Animal.hasOne('owner', Person).
I want to get an animal list with its owner info like:[{id:1 , person_id:1 ,name:dog ,owner:{id:1 ,name: jack}, {id:2 , person_id:2,name:cat ,owner:{id:2 ,name: tom}]
how can i do with node-orm2 ,thanks !!!