Why is this node.js + mongoose code not stored in the database?

I am learning Node.js + mongodb with a simple tutorial - the problem is that I cannot save it ().

This is the code I'm running:

mongoose = require('mongoose'), Schema = mongoose.Schema; PostSchema = new Schema({ title: String, body: String, date: Date }); mongoose.connect('mongodb://localhost/posterdb'); mongoose.model('Post', PostSchema); var Post = mongoose.model('Post'); // create a post and save it var post = new Post(); post.title = 'My first post'; post.body = 'Post body'; post.date = Date.now(); post.save(function(err) { console.log('error check'); if(err) { throw err; } console.log('saved'); mongoose.disconnect(); }); 

It does not print anything on the console. Any ideas?

+4
source share
2 answers

It turns out that my mongodb server did not start because I did not have the / data / db directory installed by default when installing mongo in ubuntu. Created this, started the server, everything worked fine. Solvable.

+4
source

If someone found this question for the same reason they did, then perhaps this will help:

Found that I could not save the new object in my collection. I made a schema method called validate () that interfered with the MongoDB validate () function, so why it wasn’t stored in the database and gave me zero errors. Therefore, do not name the method in your schema called validate (). I hope my weakness will save you a lot of time.

+3
source

Source: https://habr.com/ru/post/1385063/


All Articles