edit: added image of collection
edit2: after a few debugging, i tried to save a document and run the find() query, and it worked (i found the test document i saved). i think the problem now is somewhere in the connection to the db. i may be connecting to somewhere else
edit3 changed title because it doesnt seem to point to the actual problem.
after a few debugging, i found out that mongoose is querying to somewhere else aside from the database i specified in uri. when i tried to save a test document, it was saved successfully and i was able to get it back using find(). However when i used find(), it only returned the previously saved test document. So now im wondering where are these documents saved?
to add more details, here are some of the codes ive written:
apiServerRoutes.js
'use strict';
module.exports = function(app) {
var apiServer = require('../controllers/apiServerControllers');
app.route('/items')
.get(apiServer.get_all_item);
app.route('/items/:itemId')
.get(apiServer.get_item);
};
apiServerControllers.js
'use strict';
var mongoose = require('mongoose');
var Item = mongoose.model('item_m');
exports.get_all_item = function(req, res) {
console.log("get_all_item() is called");
Item.find({}, function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
exports.get_item = function(req, res) {
console.log("get_item() is called");
Item.findOne({item_typeId: req.params.typeId}, '',function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
ItemSchema.js
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ItemSchema = mongoose.Schema({
item_name: String,
item_desc: String,
item_type: String,
});
var Item = mongoose.model('item_m', ItemSchema, 'item_m');
module.exports = Item;
server.js
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Item = require('./api/models/ItemSchema'),
bodyParser = require('body-parser');
mongoose.Promise = global.Promise;
mongoose.set('debug', true);
mongoose.connect('mongodb+srv://[[user]]:[[pass]]#market-test-app-1dza9.mongodb.net/test?retryWrites=true&w=majority', {
useNewUrlParser: true, useUnifiedTopology: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection Successful!");
});
var routes = require('./api/routes/apiServerRoutes');
routes(app);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(port);
console.log('RESTful API server started on: ' + port);
end of edit3
the stuff below this are from the original question i had before i did some digging and debugging
So I have this controller function which uses a simple find query with no projection parameters
exports.get_all_item = function(req, res) {
Item.find({}, function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
Everytime I run my request in postman, I get an empty result. When I turned debug mode on in express, I saw this query sent by mongoose
Mongoose: item_m.find({}, { projection: {} })
When I try to run this query in mongodb, i get this error
"message" : ">1 field in obj: {}",
If I do add a parameter for projection eg. item_name, mongoose sends this:
Mongoose: item_m.find({}, { projection: { item_name: 1 } })
and when run that query in mongodb, i get this error
"message" : "Unsupported projection option: projection: { item_name: 1.0 }",
But when I change the query to
db.item_m.find({}, { item_name: 1 } )
It works fine, returning the results that Im expecting.
Im still new to both express, node.js, and mongodb. Is this some version problem between mongodb and mongoose?
Im using 4.0.13 for mongodb and 5.8.3 for mongoose but from what I researched, this should be fine. What other stuff am I missing here? Or what ever stuff should I check into that I may have missed?
Thanks in advance.
I use fetch all query using this method. I have tweaked it for your need.
exports.get_all_item = (req, res, next) => {
Item.find()
.select("item_name")
.exec()
.then(items => {
if (items.length > 0) {
res.status(200).json(items);
} else {
res.status(204).json({
message: "No items available"
});
}
})
.catch(error => {
next(error);
});
};
Hope it helps :)
When I checked back in my DB using mongodb atlas, I found that there were two existing databases: master (where I keep my data) and test (im not sure how this was created, but it existed). The entire time mongoose was accessing test and not master. I changed the 'test' in my URI to 'master' and it worked fine.
So I guess the lesson here is to double check the URI; and when debugging, try saving a sample data and find where that data is saved.
Related
I am trying to insert a sub document into all existing documents in a collection in db,in nodejs using express framework.Following is the code snippet:
updatedoc: function(update,options,cb)
{
return this.update({},update,options).exec(cb);
}
where parameters update and options are as follows :
const update = { $push: { "defaultads": content }};
const options = { multi: true};
It seems to run and gives the following output on the console :
{ n: 1, nmodified: 1, ok: 1 }
but no push takes place at all ,in any of the documents of the database.
I have checked :
1) whether i am pushing in the right db.
2) whether correct values are being passed
However I am not able to find where I am going wrong.
I am new to nodejs and would really appreciate guidance in solving this problem.
Thanks in advance.
I am giving a simple code with full fledged requirement of yours. First create a config.js using this file you will be connected to mongodb.Here is the code
module.exports = {
'secretKey': '12345-67890-09876-54321',
'mongoUrl' : 'mongodb://localhost:27017/product'
}
Next create a models folder . Keep this schema in this models folder . I named it as product.js. Here is the code
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var imageSchema = new Schema({
imagepath:{
type:String
}
});
var nameSchema = new mongoose.Schema({
productName:{type: String},
productPrice:{type: Number},
imagePaths:[imageSchema]
});
module.exports = mongoose.model("product", nameSchema);
Next create a routes folder and keep this routes code in this folder I named it as route.js. Here is the code
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Product = require('../models/product');
var app = express();
var Router = express.Router();
Router.use(bodyParser.json());
Router.get('/product',function(req,res){
Product.find({}, function (err, product) {
if (err) throw err;
res.json(product);
});
})
Router.post('/productData',function(req, res, next){
Product.create(req.body, function (err, product) {
if (err) throw err;
console.log('Product Data created!');
var id = product._id;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Added the product data with id: ' + id);
});
})
Router.post('/subdocument',function (req, res, next) {
Product.find({},function (err, result) {
if (err) throw err;
for(var i=0;i<result.length;i++){
result[i].imagePaths.push(req.body);
result[i].save(function (err, ans) {
if (err) throw err;
console.log('SubDocument created!');
});
}
res.send("Successfully added");
});
})
module.exports = Router;
Next server code I named it as app.js. Here is the code
var express = require('express');
var bodyParser = require('body-parser');
var Product = require('./models/product');
var mongoose = require('mongoose');
var config = require('./config');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("Connected correctly to server");
});
var app = express();
var route=require('./routes/route');
app.use('/route',route);
app.listen(3000,function(){
console.log("Server listening on 3000");
});
Run the server as node app.js.
API's
Use GET method http://localhost:3000/route/product .This is for getting all the product information.
Use POST method http://localhost:3000/route/productData .This for creating document. Post data in json format through request body like
{
"productName" : "sweets",
"productPrice" : "33"
}
You will see the response like this.
First post some documents i.e., post 2 or 3 documents and then you can see the documents with get API as I mentioned above. Then you will see all the documents contains empty sub document. You will see the response like this
And now add the sub document to all using the below api.
Use POST method http://localhost:3000/route/subdocument . Using this you can add a sub document to all the documents like this you need to add sub document
{
"imagepath" : "desktop"
}
You can see the response like this
After this again when you run the get API you can see all the sub documents are added to all documents.
Hope this helps.
I am trying to load data from my database named users which contains 2 users in the collection named userlist. Each entry has a name and age.
The code below resides in my app.js.
//set database connection
mongoose.connect('mongodb://localhost:27017/users');
mongoose.connection.on('error',function(){
console.log('MongoDB Connection Error. Please make sure that MongoDB is running');
process.exit(1);
});
mongoose.connection.once('open',function(callback){
console.log('Connected to Database');
});
var userSchema = new mongoose.Schema({
name:{type:String,uppercase:true},
age:Number
});
var userModel = mongoose.model('userModel',userSchema);
app.get('/users', function(req, res, next) {
userModel.find({},function(err,userModel){
res.send(userModel);
//Have tried res.send(userlist) also
})
});
This returns an empty set [] on localhost:3000/users. However i do have two entries in my database.
var userSchema = new mongoose.Schema({
name:{type:String,uppercase:true},
age:Number
},{collection:'userlist'});
This is what helped me.
I am writing a very simple application with NodeJS and Mongoose.
If I disable authentication in Mongoose, everything works fine and I can access my records from the database. But when I turn on the authentication and configure my NodeJS code to use authenticated Mongoose connection it doesn't let me query my records and the web page keeps on loading.
Name of my database is "bears".
P.S. I have created my users in "Admin database and Bears" database. I have given a user of books database readwrite permissions and it works fine when I authenticate it through "Mongo" command or db.auth command. But it is not working through NodeJS/Mongoose.
Here is my code.
var mongoose = require('mongoose');
var opt = {
user: 'bearsdev',
pass: 'bearsdev123!',
auth: {
authdb: 'bears'
}
};
mongoose.connect('mongodb://localhost:27017/bearsdev',opt);
var Schema = mongoose.Schema;
var BearSchema = new Schema({
name: String
});
module.exports = mongoose.model('Bear', BearSchema);
In my server.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var Bear = require('../models/bear');
var bear = require('express').Router();
bear.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
bear.get('/bear',function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
app.use('/test', bear);
app.listen(8080);
localhost:8080/test works.
localhost:8080/test/bear keeps on loading.
I have tried different ways of authentication with Mongoose, e.g.
mongoose.connect('mongodb://bearsdev:bearsdev123!#localhost:27017?authSource=bearsdev');
and
mongoose.connect('mongodb://bearsdev:bearsdev123!#localhost:27017/bearsdev');
None of these ways are working for me.
I hope following code may be work.
database = {
host: 'localhost',
db: 'bears',
port: '27017',
options: {
user: "bearsdev",
pass: "bearsdev123!",
auth: {
authdb: 'admin'
}
}
}
mongoose.connect(database.host, database.db, database.port, database.options, function (err) {
if (err) {
console.log("connection error:", err);
} else {
console.log("MongoDB connection successful");
}
});
I was able to solve the problem by uninstalling local node module of Mongoose and then installing the latest version. Probably some intermediate version (~3.6.13) had that bug. But latest Mongoose is good to go.
Nothing important
My first question here on stackoverflow. I've used it for years to find answers, but now I need a bit of guidance. I'm new to node and express and the async way of structuring an app.
Goal - A REST interface with validation and neDB database
I got the following code working. POST a new user is the only route. It's based on many answers and tuts mixed together. I find it hard to scaffold out the logic, to get a structure you can build on.
I'm not sure at all whether this structure is crap or not. Any advice would be appreciated.
Main file is initializing the database, middleware validator, and starting the app.
// rest.js
var express = require('express'),
bodyParser = require('body-parser'),
validator = require('express-validator'),
db = require('./database/db'),
userRouter = require('./routers/users');
db.init();
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(validator());
app.use('/api/users', userRouter);
var port = process.env.PORT || 8080;
app.listen(port);
Database
This question/answer made me create the small database module with alterations.
How do you pass objects around node express application?
It doesn't have much attention. Maybe because it's very obvious or maybe not a good answer.
The idea is that whem i get multiple collections, they all get initialized on startup, but I can request a single collection if that's all the module needs, or I can get the entire db object back if another module would require that.
// database/db.js
var nedb = require('nedb');
var db = {};
db.init = function() {
db.users = new nedb({ filename: './database/data/users', autoload: true });
db.users.ensureIndex({ fieldName: 'username', unique: true }, function (err) {});
db.users.ensureIndex({ fieldName: 'email', unique: true }, function (err) {});
};
db.get = function(collection) {
if (collection && db[collection])
return db[collection];
return db;
}
module.exports = db;
Router
I require the User Model here and use the express-validator and sanitizes the request before passing it on to the model, based on a minimalist key schema in the model. I don't have any controllers. If I had (or when I do), I would put the validation there. The router is supposed to send the response and status right?
// routers/users.js
var express = require('express'),
_ = require('lodash'),
User = require('../models/user');
var userRouter = express.Router();
userRouter.route('/')
.post(function(req, res) {
req.checkBody('username', 'Username must be 3-20 chars').len(3,20);
req.checkBody('email', 'Not valid email').isEmail();
req.checkBody('password', 'Password must be 6-20 chars').len(6,20);
var err = req.validationErrors();
if (err) {
res.status(422).send(err);
return;
}
var data = _.pick(req.body, _.keys(User.schema));
User.create(data, function (err, newData) {
if (err) {
res.status(409).send(err);
} else {
res.status(201).send(newData);
}
});
});
module.exports = userRouter;
Model
The model requires the database module and gets the "connection". Is this OK?
// models/user.js
var db = require('../database/db');
var User = function (data) {
this.data = data;
};
User.schema = {
_id: null,
username: null,
email: null,
password: null
};
User.create = function (data, callback) {
db.get('users').insert(data, callback);
};
module.exports = User;
Thanks for reading this far. Now, my question is:
Is there something fundamentally wrong with this setup, concerning the database usage and the validation logic. I know the model looks stupid :)
I have created nodejs(expressjs) application with mongodb on Openshift.
and I have pushed my database (mehendiDB) on the mongodb server which I can see on the server by using rockmongo cartridge as follows
admin(2)
api(3)
local(1)
mehendiDB(5)
Comments(10)
Likes(10)
Posts(8)
Users(9)
system.indexes(4)
Though I can see the data uploaded onto server but when I retrive it with the following code from my users.js I do not get anything but an empty array. Code I ave written in users.js is as follows
var express = require('express');
var router = express.Router();
var mongodb = require('mongodb');
router.dbServer = new mongodb.Server(process.env.OPENSHIFT_MONGODB_DB_HOST,parseInt(process.env.OPENSHIFT_MONGODB_DB_PORT));
router.db = new mongodb.Db(process.env.OPENSHIFT_APP_NAME, router.dbServer, {auto_reconnect: true});
router.dbUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME;
router.dbPass = process.env.OPENSHIFT_MONGODB_DB_PASSWORD;
router.db.open(function(err, db){
if(err){ throw err };
router.db.authenticate(router.dbUser, router.dbPass, {authdb: "admin"}, function(err, res){
if(err){ throw err };
});
});
router.get('/', function (req, res){
router.db.collection('Users').find().toArray(function(err, names) {
console.log("Printing output : " + names);
res.header("Content-Type:","application/json");
res.end(JSON.stringify(names));
});
});
module.exports = router;
PS : I am not getting any errors in the log console(nodejs.log) when I checked it. and showing 'Printing output'
Thanks in advance.
Have you verified that the database name that you uploaded your data to is the same as the process.env.OPENSHIFT_APP_NAME that you are using within your application?