Problem wtih models

54 views
Skip to first unread message

toni...@gmail.com

unread,
Jan 19, 2014, 2:42:54 AM1/19/14
to tot...@googlegroups.com
Hi Peter ,

I'm using Postgresql in one app that i'm writing ,but the model of product-family always return "undefined"

model products.js

exports.data = function(self, jsonData) {
return {
id : jsonData.codex,
name : jsonData.name,
namefamily : framework.module('model')('families').name(self,
jsonData.codfamily)
};
};

and model families.js

exports.name = function(self, codfamily) {

var db = self.database();
db.connect(function(err) {
db.query("select * from families where codex='"
+ codfamily + "'", function(err, result) {
db.end();
name=result;
});
});
return name;
};


How i can response with the result of the db.query?


Thanks a lot for a response...


Peter Širka

unread,
Jan 19, 2014, 2:52:17 AM1/19/14
to tot...@googlegroups.com, toni...@gmail.com
Hi,
I need more informations:

- Where do you have saved models (directory)?
- How do you obtain a model?

Thanks!

toni campins

unread,
Jan 19, 2014, 4:13:38 AM1/19/14
to Peter Širka, tot...@googlegroups.com
Hi,

- I save models in models directory 


- Model product 
var product = framework.module('model')('products').data(self,data)

exports.data = function(self, jsonData) {
return {
id : jsonData.codex,
familideno : framework.module('model')('families').name(self,
jsonData.codex)
};
};

My problem is in families.js , i need return var result ,but i don know how it ia done

exports.name = function(self, codex) {
var db = self.database();
var description = function(codex) {
db.connect(function(err) {
db.query("select * from families where idej=9 and codex='"
+ codex + "'", function(err, result) {
db.end();

                                console.log(result);
                                
});
});

return result.name;
};
return description(codex);

}

The postgresql connection: 
        pg = require('pg');

framework.database = function() {
return new pg.Client('postgres://postgres:password@localhost:5433/dbName');
};












2014/1/19 Peter Širka <peter...@gmail.com>

Peter Širka

unread,
Jan 19, 2014, 6:12:58 AM1/19/14
to tot...@googlegroups.com, Peter Širka, toni...@gmail.com
You must create a callback :-(

exports.name = function(self, codex, callback) {
 
....
 
....
  callback
(result);
}

... because all DB operation are asynchronous.

self.module('model')('my-model').data(self, codex, function(result) {
   console
.log(result);
});

Thanks.

Peter Širka

unread,
Jan 26, 2014, 10:37:14 AM1/26/14
to tot...@googlegroups.com, toni...@gmail.com
Hi guys,
new version will contain models in framework core.

Thanks.
Reply all
Reply to author
Forward
0 new messages