Node.js orm2 – 检索OneToMany关系中的新关联的麻烦
我正在使用node-orm2来感受在RESTful API中使用的感觉。 我在后端使用Postgres数据库。
我试图在他们的文档上的一个变体,我有一个人和一个任务之间的OneToMany关系与一个单一的文件中的以下内容:
app.js
'strict' // Based on http://blog.modulus.io/nodejs-and-express-create-rest-api // but with my own twists! var express = require('express'); var app = express(); var orm = require('orm'); // Helpers var collectionize = function(target){ if(typeof target == 'Object'){ return [target]; } return target; }; // Configure the app with any middleware app.use(express.bodyParser()); app.use(orm.express("postgres://rodrigomartell:password@localhost/postgres",{ define : function(db, models, next){ // Define models models.Person = db.define("Person", { name : String, surname : String, age : Number, male : Boolean, continent: ["enum1", "enum2"], data : Object }, { methods : { fullName: function(){ return this.name + this.surname; } }, validations : { name: orm.enforce.unique("name already taken!"), age : orm.enforce.ranges.number(18, undefined, "under-age") }, autoFetch : true // global eager load } ); models.Task = db.define("Task", { description: String } ); // Relations models.Task.hasOne('person', models.Person, {reverse:'tasks', required: true}); // Finally drop and sync this puppy db.drop(function(){ db.sync(function(){ console.log('All good'); // Create one Person on load db.models.Person.create([{ name : "Kenny", surname : "Powers", age : 34, male : true, continent: "enum1" }], function(err, items){ var person = items[0]; db.models.Task.create([{description:'Heyo!', person_id: person.id}], function(err, tasks){ console.log(err,tasks); }); }); // Press on? next(); // After synching, get on with the rest of the app? },function(err){ console.log('No good', err); }); }); } })); // Configure the routes and that app.get("/people", function(req,res){ console.log('requested'); res.type('text/plain'); // req.models is a reference to models in the middleware config step req.models.Person.find(null, function(err, people){ // There must be a neater way to findAll? res.json(people); }); }); app.post("/people", function(req,res){ console.log('requested'); res.type('text/plain'); req.models.Person.create(collectionize(req.body), function(err, newPerson){ if(err){ res.json({error: err}); } else{ res.json(newPerson); } }); }); app.get("/tasks", function(req, res){ res.type('text/plain'); req.models.Task.find(null, function(err, tasks){ // There must be a neater way to findAll? res.json(tasks); }); }); app.post("/tasks", function(req, res){ res.type('text/plain'); var task = req.body; req.models.Task.create([req.body], function(err, task){ if(err){ res.json(err); } else{ res.json(task); } }); }); // Listen up! app.listen(4730, function(){ console.log('Listening on http://localhost:4730'); });
我为以下资源设置了GET和POST路由:
-
{GET,POST} /人
-
{GET,POST} /任务
在数据库的初始加载时,我创build了一个Person实例,只是为了在DB中有一些东西。
我可以在localhost:4730 / people上执行一个GET操作,然后找回在加载时创build的人员的任务(awesome):
[{ name : "Kenny", surname : "Powers", age : 34, male : true, continent: "enum1", data : null, id : 1, tasks : [ { description: "Heyo!", id: 1, person_id: 1 } ] }]
如果我在localhost:4730 /任务上执行GET ,则按照预期返回:
[ { description: "Heyo!", id: 1, person_id: 1 } ]
现在,这是我的问题的开始:
如果我做一个POST到localhost:4730 /有效载荷的任务 :
{ description: "Another task", person_id: 1 }
然后做一个新的GET 本地主机:4730 /任务我得到这个按预期:
[{ description: "Heyo!", id: 1, person_id: 1 }, { description: "Another task", id: 2, person_id: 1 }]
现在,在localhost:4730 /人上做一个新的GET , 人们可以期望显示分配给person_id 1(Kenny Powers)的两个任务,但是,可惜的是,这个协会似乎已经在多方面登记,而不是在关系的一方:
[{ name : "Kenny", surname : "Powers", age : 34, male : true, continent: "enum1", data : null, id : 1, tasks : [ { description: "Heyo!", id: 1, person_id: 1 } ] }]
我不能从我可能会出错的文档中找出答案。 有没有人遇到类似的问题?
谢谢。
真棒问题,我也找不到任何东西。 确定没有设置caching,而是清理它,通过git仓库后,我发现这个:
orm.singleton.clear(instance._singleton_uid);
它可以工作,但是很脏。 可能帮助别人。
更新:
你可以设置instance.cacheSaveCheck
为true,这也将修复它!
我只是检查这个陈旧的问题,看到我得到了一个风滚草徽章。 不知道这是好还是不好(一分钟就能谷歌),但我会把它作为好。
无论如何,我只是玩这个更多,发现这个post ,这导致我尝试在人模型中closurescachingclosures(默认为true)。
所以,如果在Person模型中,在autoFetch之后添加一个新属性来将caching设置为false,那么这一切都可以工作。 它应该是这样的:
models.Person = db.define("Person", { name : String, surname : String, age : Number, male : Boolean, continent: ["enum1", "enum2"], data : Object }, { methods : { fullName: function(){ return this.name + this.surname; } }, validations : { name: orm.enforce.unique("name already taken!"), age : orm.enforce.ranges.number(18, undefined, "under-age") }, autoFetch : true, // global eager load cache : false // Important as otherwise it doesn't update the relationship, why? } );
我不能说我完全理解为什么这样做会尽我所能挖掘它,但很高兴它是固定的。
我希望它能帮助任何人在这里遇到类似的问题。 我现在要在一个空的沙漠场景中向我的想象中的朋友炫耀我的新的风滚草徽章。