Save function does nothing and gives no error

6,612 views
Skip to first unread message

yamafaktory

unread,
Feb 6, 2011, 9:20:15 AM2/6/11
to Mongoose Node.JS ORM
Hi everyone,

Trying to do some basic stuff with mongoose, but the following code
does nothing at all, even with a local mongoDB install (I'm on windows
7 64bit / cygwin / node v0.2.6)

var mongoose = require('mongoose');

mongoose.connect('mongodb://xxx:y...@flame.mongohq.com:27082/
mydatabase');// or mongodb://127.0.0.1:27017/mydatabase

var Schema = mongoose.Schema;

var mySchema = new Schema ({
username : { type : String, unique : true},
email : String,
password : String,
date : { type : Date, default : Date.now }
});

mongoose.model('UserSchema', mySchema);

var UserSchema = mongoose.model('UserSchema');

var thisUser = new UserSchema({username : 'mongo', email :
'te...@gmail.com', password : 'something'});

thisUser.save(function(err){
console.log(err);
});

It gives me no error at all and nothing appears in the database...

Thanks a lot!

Tartifletteman

unread,
Feb 7, 2011, 3:43:41 AM2/7/11
to Mongoose Node.JS ORM
Try this to retrieve your model :
app.UserSchema = UserSchema = mongoose.model('UserSchema');

Your code wasn't working in my environment either, but with this it
worked fine.
Not sure why...

Does this solve your problem ?

yamafaktory

unread,
Feb 8, 2011, 1:19:39 AM2/8/11
to Mongoose Node.JS ORM
Using app.UserSchema = UserSchema = mongoose.model('UserSchema');
gives me now this error : Cannot set property of 'UserSchema' of
undefined

Guillermo Rauch

unread,
Feb 8, 2011, 1:27:10 AM2/8/11
to mongoo...@googlegroups.com
You sure `app` is not undefined?
--
Guillermo Rauch
http://devthought.com

yamafaktory

unread,
Feb 8, 2011, 7:14:04 AM2/8/11
to Mongoose Node.JS ORM
Finally no more 'undefined' error (in fact, I made a bad insertion of
the line in code)... but still nothing's going on.

Siegfried Ehret

unread,
Feb 8, 2011, 7:40:57 AM2/8/11
to mongoo...@googlegroups.com
After the line where you do the connection to the database, can you add these lines : 
console.log(mongoose.connection.host);
console.log(mongoose.connection.port);

I run a local db on my debian (virtualbox powered ;)) and I have these values :
localhost
27017

Can you give the result ?
Maybe your connection to the db is weird and I hope (is the connection fails for example) that these values will be undefined.
Message has been deleted
Message has been deleted

yamafaktory

unread,
Feb 9, 2011, 3:25:41 AM2/9/11
to Mongoose Node.JS ORM
Unfortunately, after having added these two lines, I get a correct and
available connection to the database :

flame.mongohq.com
27082

Moreover, when I make an extraction of the whole mongoDB thing from my
code (express app) and run it separately, I get that error message
from the save function :

{ message: 'unauthorized', stack: [Getter/Setter] }

Here's my code :

----------------------------------------------------------------------------------------------------------------------------------------------------------------

var express = require('express');

var connect = require('connect');

var mongoose = require('mongoose');

var io = require('socket.io');

mongoose.connect('mongodb://user:pa...@flame.mongohq.com:27082/
mydb'); //Of course, replaced by my own values

console.log(mongoose.connection.host);

console.log(mongoose.connection.port);

var Schema = mongoose.Schema;

var UserSchema = new Schema ({
username : { type : String, unique : true},
email : String,
password : String,
date : { type : Date, default : Date.now }
});

mongoose.model('UserSchema', UserSchema);

var app = express.createServer();

app.UserSchema = UserSchema = mongoose.model('UserSchema');
var thisUser = new UserSchema({username : 'dsfsdf', email : 'dfsdf',
password : 'sdfsdf'});
thisUser.save(function(err){
if (err) console.log(err);
else console.log('yessss');
});

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Running Turtle

unread,
Feb 9, 2011, 4:26:32 AM2/9/11
to Mongoose Node.JS ORM
I have the same problem as you. Looks like its a driver problem:

http://support.mongohq.com/discussions/problems/234-php-driver-issues

On Feb 9, 9:25 am, yamafaktory <yamafakt...@gmail.com> wrote:
> Unfortunately, after having added these two lines, I get a correct and
> available connection to the database :
>
> flame.mongohq.com
> 27082
>
> Moreover, when I make an extraction of the whole mongoDB thing from my
> code (express app) and run it separately, I get that error message
> from the save function :
>
> { message: 'unauthorized', stack: [Getter/Setter] }
>
> Here's my code :
>
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> var express = require('express');
>
> var connect = require('connect');
>
> var mongoose = require('mongoose');
>
> var io = require('socket.io');
>
> mongoose.connect('mongodb://user:p...@flame.mongohq.com:27082/

Siegfried Ehret

unread,
Feb 9, 2011, 6:32:35 AM2/9/11
to mongoo...@googlegroups.com
Your code run just fine for me :(

I don't know why a php driver problem should be in cause here :S

Running Turtle

unread,
Feb 9, 2011, 7:24:42 AM2/9/11
to Mongoose Node.JS ORM
Quote from MongoHq support staff : "Generally we see this on drivers
trying to do a db list or something like that."

I was just pointing out that other drivers were having apparently
similar issues.

yamafaktory

unread,
Feb 9, 2011, 7:32:27 AM2/9/11
to Mongoose Node.JS ORM
Seems to be a mongoHQ problem since I can get it working on my
localhost mongoDB server... Don't know why there's such an issue with
mongoHQ (and it's not a firewall issue). Regardless of this non-
related mongoose issue, thanks all for your help and your time!

Siegfried Ehret

unread,
Feb 9, 2011, 7:34:42 AM2/9/11
to mongoo...@googlegroups.com
Haha I'm glad it is not a mongoose issue ^^

yamafaktory

unread,
Feb 13, 2011, 2:02:06 AM2/13/11
to Mongoose Node.JS ORM
Since mongoose 1.0.10, MongoHQ connection is ok, no more issues!

Jon Bomgardner

unread,
Feb 13, 2011, 4:29:39 PM2/13/11
to Mongoose Node.JS ORM
Hello all,

I'm sorry to report that I'm seeing this exact same behavior but with
a local mongodb installation. Is it possible that it could be a
problem with the node version I'm using? I'm using v0.4.0 (I'm just
following master) and the master branch of mongoose.

I ran the check for the connection host and port as shown above and it
shows standard results (localhost and 27017). I also output the model
instance and the isNew property is true both before and after calling
save. The odd part is that I use a different model earlier in the
program and it works fine (returns results).

Any thoughts?

Thanks,
Jon Bomgardner

Jon Bomgardner

unread,
Feb 13, 2011, 5:52:08 PM2/13/11
to Mongoose Node.JS ORM
Also, I do have a middleware setup for this model that updates a time
stamp. I put a log statement in there to see if it makes it that far
and it does. However, it never seems to call the callback function I
passed into the save function (on either an error or success). I can
tell this because I have a log statement in there as well. I think it
must be something with my Schema because I have another model that
seems to be working just fine.

so, here's the Schema I'm using:

var mongoose = require('mongoose')
, Schema = mongoose.Schema;

var Setting = new Schema({
key: String,
value: String
});

var Settings = new Schema({
module: String,
settings: [Setting],
updated_at: Date

});

Settings.pre('save',function(next){
core.log('in save middleware');
this.updated_at = Date.now;
next();
});


mongoose.model('Setting', Settings);

Does anyone see anything wrong with this model?

Thanks,
Jon Bomgardner

Siegfried Ehret

unread,
Feb 13, 2011, 8:50:34 PM2/13/11
to mongoo...@googlegroups.com
I ran your model and it's fine.
Date.now doesn't worked for me but with new Date() it's well updated.

My code :

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mahTest'); 

var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

var Setting = new Schema({
  key: String,
  value: String
});

var Settings = new Schema({
  module: String,
  settings: [Setting],
  updated_at: Date
});

Settings.pre('save',function(next){
  console.log('presave');
  this.updated_at = new Date();
  next();
});

mongoose.model('Settings', Settings);

var SettingsModel = mongoose.model('Settings');

var setting = {'key':'myKey', 'value':'myValue'};
var settings = new SettingsModel({'module':'myModule'});
settings.settings = new Array();
settings.settings.push(setting);
settings.save(function(err){
  if (err) console.log(err);
  else console.log('yep');
});

Jon Bomgardner

unread,
Feb 13, 2011, 11:09:22 PM2/13/11
to Mongoose Node.JS ORM
If you have a moment, try running this (it's giving me a cast error -
'Cannot call method \'cast\' of undefined'):

Setting.find({'module': 'theme' , 'module.setting': 'activeTheme'},
function(err, docs){
if (err) {
console.log(err.message);
} else {
console.log('good');
}
});

and the save still isn't actually saving in the db. After running the
save, when I look at the db, I can't see a settings collection.

Thanks for the help,
Jon

Jon Bomgardner

unread,
Feb 13, 2011, 11:23:38 PM2/13/11
to Mongoose Node.JS ORM
Also, if you want to see this in context the code can be found at
github (it's an open source cms I started):

https://github.com/jonlb/node-JxCMS

The problem is in system/theme.js. As long as your mongodb instance is
on localhost it should just be able to run with:

git submodule update --init --recursive
node cms.js

then, hit the server at

http://localhost:8000/loader

and then go check your database for a default and/or dev database. The
default db should have two collections (modules and settings). In mine
the settings collection never gets created. And just FYI, this whole
thing is highly experimental at the moment.

Thanks,
Jon

On Feb 13, 5:50 pm, Siegfried Ehret <tartiflette...@gmail.com> wrote:

Charlie

unread,
Apr 4, 2011, 6:46:35 AM4/4/11
to Mongoose Node.JS ORM
I'm getting the same problem as Jon.

I'm using a middleware that does:

schema.pre('save', function() {
//Do stuff
});

When calling
model.save(function(err) {
if (err) throw err;
console.log('save callback ran');
});

The save callback never runs.

This does run:
model.on('save', function(err) {
if (err) throw err;
console.log('post save ran');
});

One other point to mention is that the error in the save callback
never gets called, even if I don't have mongod running, so there's no
way the document could have saved. Doing console.log(model) after
save shows no errors and shows isNew: false.

Guillermo Rauch

unread,
Apr 4, 2011, 1:40:55 PM4/4/11
to mongoo...@googlegroups.com
Are you calling `next` in the pre hook?

-- 
Guillermo Rauch
http://devthought.com

Charlie

unread,
Apr 4, 2011, 4:34:38 PM4/4/11
to Mongoose Node.JS ORM
Yep, Im calling an async function and next() within the callback

On Apr 4, 6:40 pm, Guillermo Rauch <rau...@gmail.com> wrote:
> Are you calling `next` in the pre hook?
>
> --
> Guillermo Rauchhttp://devthought.comhttp://learnboost.com
Reply all
Reply to author
Forward
0 new messages