building a schema mongodb - mongodb

Whilst building the following schema
'use strict';
var User = mongoose.model('checkIn')
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var checkIn = new Schema({
email: {
type: String
// default:User.local.email
},
checkInDate: {
type:Date,
default:Date.now()
}
})
module.exports = mongoose.model('User', checkIn);
I encountered the following error message
How do I fix this?

The error clearly says, "cannot read property of undefined". That means "mongoose" is undefined when it reaches var User = mongoose.model('checkIn'). Of course, because the require statement var mongoose = require('mongoose'); comes afterwards. You should put the require statement first so that 'mongoose' is available when you call model property on it.

Related

Receiving an error saying nested schema in MongoDB

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: String,
created: new Date()
})
const user = mongoose.model('user', userSchema);
module.exports = user;
After adding new Date() I get an error saying :
TypeError: Undefined type undefined at created
Did you try nesting Schemas? You can only nest using refs or arrays.
You are defining schema and you just need a type there not the object. Instead of using new Date() just use Date.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: String,
created: Date
})
const user = mongoose.model('user', userSchema);
module.exports = user;
I would guess that your intention was to have a Date field type with a mongoose default value of the current date time ... since you named that field created. I would go a step further and name it created_At ...
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: String,
created_At: { type: Date, default: Date.now }
})
const user = mongoose.model('user', userSchema);
module.exports = user;
Now you will have an auto filled created_At Date field every time you create a new user model.
We specifically said what type we want the field to be and we also added a default value.
You can read more about mongoose defaults here

mongoose model to connect to mongoDB

i have created model for the mongo collection like below. but it was giving me the collection output which saved in mongoDB.
var mongoose = require('mongoose'),
Schema = mongoose.Schema({
name: {
type: String
},
age: {
type: Number
},
})
module.exports = mongoose.model('container', Schema);}
But later when i changed the last line of the code which is
"module.exports = mongoose.model('container', Schema);"
to
"module.exports = mongoose.model('container', Schema, 'container');"
it worked properly. I check the mongoose document they say to use the previous line, then why didn't it worked.
your problem seems to be from using "Schema" as a variable name
var ContainerSchema = new mongoose.Schema({
...
});
and exporting
module.exports = mongoose.model("Container", ContainerSchema);
would work.

Connected to MongoDB but Express .get returns 404 error?

I have an app using MongoDB + Mongoose on Express v4. I believe I have successfully connected my database, but my GET requests are returning a 404 error when I test the connection on Postman.
Could someone spot an error in my code?
Server.js
...
var mongo = require('mongodb');
var mongoose = require('mongoose');
require('./models/Thread');
require('./routes/thread');
mongoose.connect('mongodb://localhost:27017/pudo');
var app = express();
var router = express.Router();
...
/models/Thread.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ThreadSchema = new mongoose.Schema({
name: String
});
var Thread = mongoose.model('Thread', ThreadSchema);
module.exports = Thread;
/routes/thread.js
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Thread = mongoose.model('Thread');
router.get('/threads', function(req, res, next){
Thread.find(function(err, threads){
if(err){return next(err);}
res.json(threads);
});
});
module.exports = router;
I believe this is the minimal connections between my code in order for it to work. However, sending a GET to /threads returns a 404 error instead of any database records.
I found the solution! Instead of:
require('./routes/thread');
Use this:
router.use(require('./routes/thread'));

Subdocument based on another document

I was wondering if it's possible in Mongoose to have a subdocument based on another document.
Normally I would do something like this:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CountrySchema = new Schema({
name: String,
info: String,
cities : [
{
type: Schema.ObjectId,
ref: 'Ressource'
}
]
});
module.exports = mongoose.model('Country', CountrySchema);
this however create a new document and then references it via the id to the country document. This is not what I want, I want the resource document to be nested inside the country document. How would I do this?
I found that this fixed my problem:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ressourceSchema = require('../ressource/ressource.model');
var CountrySchema = new Schema({
name: String,
info: String,
cities : [
ressourceSchema.schema
]
});
module.exports = mongoose.model('Country', CountrySchema);

Mongoose.js instance.save() callback not firing

var mongo = require('mongoose');
var connection = mongo.createConnection('mongodb://127.0.0.1/test');
connection.on("error", function(errorObject){
console.log(errorObject);
console.log('ONERROR');
});
var Schema = mongo.Schema;
var BookSchema = new Schema({ title : {type : String, index : {unique : true}}});
var BookModel = mongo.model('abook', BookSchema);
var b = new BookModel({title : 'aaaaaa'});
b.save( function(e){
if(e){
console.log('error')
}else{
console.log('no error')
}});
Neither the 'error', or 'no error' are printed to the terminal. What's more the connection.on 'error' doesn't seem to fire either. I have confirmed that MongoDb is running.
this is a case where you are adding the model to the global mongoose object but opening a separate connection mongo.createConnection() that the models are not part of. Since the model has no connection it cannot save to the db.
this is solved either by connecting to mongo on the global mongoose connection:
var connection = mongo.createConnection('mongodb://127.0.0.1/test');
// becomes
var connection = mongo.connect('mongodb://127.0.0.1/test');
or by adding your models to your separate connection:
var BookModel = mongo.model('abook', BookSchema);
// becomes
var BookModel = connection.model('abook', BookSchema);
I really like Aaron's answer, and thanks to him I am now on my way to fixing the issue... although I'm not there yet! Here is my particular issue:
I want to have my schema and models defined in separate files, so I can reuse them from project to project. So as an example I have a file named W8DBItem.js as follows:
var mongoose = require('mongoose');
var itemSchema = new mongoose.Schema({ name: {type: String, required: true}});
module.exports = mongoose.model('W8DBItem', itemSchema);
In my program file I do this this:
var mongoose = require('mongoose');
var W8DBItem = require('../w8/W8DBItem.js');
var dbURL ='mongodb://localhost:27017/default';
var mongoOptions = { useNewUrlParser: true, bufferCommands: false }
mongoose.connect(dbURL, mongoOptions);
var db = mongoose.connection;
// DEAL WITH CONNECTION ERROR
db.on('error', console.error.bind(console, 'connection error:'));
// PREP DATA
var aWeight = { name: "My Test Name" };
var newWeightItem = W8DBItem(aWeight);
// CONNECTION ESTABLISHED
db.once('open', function() {
console.log("Here 1")
// TRY TO SAVE
newWeightItem.save(function (err, newWeightItem) {
if (err) {
console.log("Here 2");
console.log(err);
}
else {
console.log("Here 3");
console.log(newWeightItem);
}
});
});
When I run this program I get "Here 1" but never "Here 2" or "Here 3" in the console.
From Aaron's post I get that the W8DBItem object has no associated (and open) connections, but I am not sure how to go about fixing things. I could connect to the DB in the W8DBItem.js file, but I really don't like hard-coding the server info with the objects - I want these objects to be used in different files, and perhaps with different servers.
Ideas and suggestions are much appreciated!
[EDIT: SOLUTION FOUND!!!]
Instead of exporting my mongoose.model from my object file, I am only exporting the schema:
var mongoose = require('mongoose');
var itemSchema = new mongoose.Schema({name: {type: String, required: true}});
module.exports = itemSchema;
In my program files I then do this:
var itemSchema = require('../w8/W8DBItemSchema.js');
...
var W8DBItem = db.model('W8DBItem', itemSchema);
var newWeightItem = W8DBItem(aWeight);
...
Works like a charm. I hope this helps someone!
The posted answer does not solve the problem. Unfortunately, I cannot just upgrade my database, so that is not a solution either for me. But here I found a solution to this problem: https://github.com/Automattic/mongoose/issues/4064
Just use .$__save instead of .save as shown:
var b = new BookModel({title : 'aaaaaa'});
b.$__save({}, function(e){
if(e){
console.log('error')
// callback will show if e exists
}else{
console.log('no error')
// callback will show 'no error'
}});