appengine / db question

3 views
Skip to first unread message

Roberto Saccon

unread,
Oct 24, 2009, 5:40:21 PM10/24/09
to nitro-devel
After having messed around with the template stuff, I am starting to
dig now into the db stuff, but with little success yet. I am trying to
setup a very simple contact form, but something goes wrong, I am
getting an error message like this:

TypeError: Cannot read property "name" from undefined : at /Users/
rsaccon/...../api/datastore/types.js:38



here is the relevant part of what I am doing in the controller:


....
var params = new Request(env).params();

print(params.name); // gives expected result !!!

var contact = new Contact(params.name, params.email);

contact.company = params.company;
contact.website = params.website;
contact.subject = params.subject;
contact.message = params.message;
contact.created = new Date();
contact.put();
...


and here is the relevant part of what I have in the model:

....
var db = require("google/appengine/ext/db")

var Contact = exports.Contact = function(name, email, company,
website, subject, message) {
this.name = name;
this.email = email;
this.company = company;
this.website = website;
this.subject = subject;
this.message = message;
this.created = new Date();
this.__key__ = db.key();
};

Contact.model = new db.Model(Contact, "Contact", {
name: new db.StringProperty({required: true}),
email: new db.StringProperty({required: true}),
company: new db.StringProperty(),
website: new db.StringProperty(),
subject: new db.StringProperty(),
message: new db.TextProperty({required: true}),
created: new db.DateTimeProperty({autoNowAdd: true})
});
....

I tried to do the same as in blog-gae example, and it looks to me that
there is some redundancy in controller and model which does not make
much sense to me, but the main problem, of course, is that this code
does not work. Anything obvious I misunderstood ?

--
Roberto

George Moschovitis

unread,
Oct 25, 2009, 2:34:45 AM10/25/09
to nitro...@googlegroups.com
Hello,

   ....
   var params = new Request(env).params();

   print(params.name);  // gives expected result !!!

   var contact = new Contact(params.name, params.email);

   contact.company = params.company;
   contact.website = params.website;
   contact.subject = params.subject;
   contact.message = params.message;
   contact.created = new Date();
   contact.put();
   ...


and here is the relevant part of what I have in the model:

   ....
   var db = require("google/appengine/ext/db")

   var Contact = exports.Contact = function(name, email, company,
website, subject, message) {
       this.name = name;
       this.email = email;
       this.company = company;
       this.website = website;
       this.subject = subject;
       this.message = message;
       this.created = new Date();
       this.__key__ = db.key();

should be something like:

this.__key__ = db.key({kind: this.constructor.kind()});
 
   };


   Contact.model = new db.Model(Contact, "Contact", {

should be something like:

db.Model.extend(Contact, "Contact", {
 
       name: new db.StringProperty({required: true}),
 
 
I tried to do the same as in blog-gae example, and it looks to me that

there is some redundancy in controller and model which does not make
much sense to me, but the main problem, of course, is that this code
does not work.

Can you elaborate on the redundancy?

regards,
-g.


PS: I would suggest that you wait for a few days before experimenting with datastore. I am about to release an improved version comes as close as possible to the python api.


--
blog.gmosx.com

George Moschovitis

unread,
Oct 25, 2009, 6:03:44 AM10/25/09
to nitro...@googlegroups.com
   var contact = new Contact(params.name, params.email);

   contact.company = params.company;
   contact.website = params.website;
   contact.subject = params.subject;
   contact.message = params.message;
   contact.created = new Date();
   contact.put();
   ...


You could use google/appengine/ext/db/forms for this:

var ModelForm = require("google/appengine/ext/db/forms").ModelForm,
    ContactForm = ModelForm(Contact);

...


    var contact = params.key ? Contact.get(params.key) : new Contact();    
    var form = new ContactForm(params, {instance: contact});

...

    form.put();

again, I will update the blog-gae example to reflect these features....

-g.



--
blog.gmosx.com

Roberto Saccon

unread,
Oct 25, 2009, 5:15:40 PM10/25/09
to nitro-devel
thanks, now things make a bit more sense and will wait for the updated
example

On Oct 25, 11:03 am, George Moschovitis <george-moschovi...@gmosx.com>
wrote:
Reply all
Reply to author
Forward
0 new messages