I'm a total beginner at this and am using a tutorial to learn the basics of the MEAN stack. I am trying to return the documnents in my database to a web page but am instead receiving an empty array.
I have created a cluster on Mongodb Atlas called mytasklist. Inside here I created a database called mytasklistdb. Inside this I have a table (object) called mytasklistdb.mytasklisttutorial. My understanding of this is limited and so maybe I'm making a huge error somewhere here. I have experience of SQL but not Mongo and so the whole 'clusters' and 'collections' thing is new to me.
Anyway my code is as follows. I took the string for the database connection from the Mongo connection tab.
var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb+srv://myusername:mypassword#mytasklist-qx0ka.mongodb.net/test?retryWrites=true&w=majority', ['mytasklisttutorial']);
router.get('/tasks', function(req, res, next){
db.mytasklistdb.find(function(err, tasks){
if(err){
res.send(err);
}
res.json(tasks);
});
});
module.exports = router;
My database objects look like this:
_id:5db5f1f31c9d440000c3e7fe
title:"Walk the dog" - this is a string
isDone:false - this is boolean
I'm just getting an empty array but in the tutorial the guy is getting these 'documents'. What am I doing wrong?
EDIT: I realised that the 'tasks' part of the tutorial example was relating to a database called 'tasks'. Mine is called 'mytasklistdb'. I therefore changed this. I also added a parameter with the name of my collection to the line passed in to mongojs.
I have changed my code above to reflect this
The solution was to replace 'task' and 'test' with the name of my db. As follows:
var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('mongodb+srv://James:Noentry1#mytasklist-qx0ka.mongodb.net/mytasklistdb?retryWrites=true&w=majority', ['mytasklisttutorial']);
router.get('/tasks', function(req, res, next){
db.mytasklisttutorial.find(function(err, tasks){
if(err){
res.send(err);
}
res.json(tasks);
});
});
module.exports = router;
My guess is that you are not passing your query, just the callback in the find() method, probably you need to do something like this:
db.tasks.find({},function(err, tasks){
if(err){
res.send(err);
}
res.json(tasks);
});
Related
Why does res.json respond with an empty array?
I want to get all data from collection distributor ,so for that I have
created model file and then assigned it to on variable Distributordata
Server file : app.js
var express=require('express');
var app=express();
var bodyParser=require('body-parser');
var mongoose=require('mongoose');
var Distributordata=require('./distributor.model');
var db='mongodb://localhost/Distributordata';
mongoose.connect(db);
var port=8080;
app.get('/',function(req,res){
res.send("happy to be here");
});
app.get('/distributor',function(req,res){
// res.send("hi from distributors")
console.log("getting all distributors");
Distributordata.find(function(err,distributordata){
if(err){
res.send("error has occured");
}
else{
console.log(distributordata);
res.send(distributordata);
}
});
});
app.listen(port,function(){
console.log('app listening on port'+port);
});
Model file : distributor.model.js
var mongoose=require('mongoose');
var Schema = mongoose.Schema;
var DistributorSchema=new Schema({
dname:String,
daddress: String
});
module.exports=mongoose.model('Distributordata',DistributorSchema);
output
on cmd:
app listening on port8080
getting all distributors
[ ]
So far I've understood your problem is you are creating your collection in mongodb by yourself as the collection name distributor. And also as you are not sending any post request so your code will not create any collection automatically in the main database.
But, by default mongoose always creates a collection name by pluralizing the name of the model, in your case this collection name would be distributordatas. But when, you are trying to get the data from your database, it's getting the collection name as distributor for this reason it becomes unable to make a connection. But you can keep this using force naming convention for collection as like you want.
In your distributor.model.js file change this line of code to this one:
module.exports = mongoose.model('Distributordata', DistributorSchema, 'distributor');
Hopefully, it'll solve your problem. For more reference check this out https://mongoosejs.com/docs/guide.html#collection
So I'm trying to pass in variables through the navigation bar for my router.
I'm using Node.js with express and mongolab for the database for this.
Here is the working code:
router.get('/sort/50/time', function(req, res) {
var db = req.db;
var collection = db.get('twots');
collection.find({},{'skip':0, 'limit':50, 'sort':{_id: -1}},function(e,docs){
res.json(docs);
});
});
It gives me a database list from mongolab.
But I want to do something like this:
router.get('/sort:VARIABLE2:VARIABLE1', function(req, res) {
var db = req.db;
var collection = db.get('twots');
collection.find({},{'skip':0, 'limit':req.params.VARIABLE2, 'sort':{req.params.VARIABLE1: -1}},function(e,docs){
res.json(docs);
});
});
This doesn't work, and can't find the correct code for this. (it can't find req.params.VARIABLE1 or VARIABLE2)
Right now I've coded the hard way, using no variables.
You still have to include the slashes:
router.get('/sort/:VARIABLE2/:VARIABLE1', function(req, res) { ... })
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?
I am trying to create a login system where an admin can log in and look at data that is not available to the public. Right now I'm just trying to find my admin instance in my database. I have two node files: app.js and account_manager.js
app.js
//creates node app
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var AM = require('./account_manager');
//stuff...
/***************************************
LOGIN
***************************************/
app.post('/login', function(req, res){
AM.manualLogin(req.param('username'), req.param('password'), function(e, o){
if (!o){
res.send(e, 400);
} else{
req.session.user = o;
res.send(o, 200);
}
});
});
account_manger.js is required in app.js and is stored in AM
account_manager.js:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost');
var admin_db = mongoose.connection;
admin_db.on('error', console.error.bind(console, 'connection error: '));
//connection open?
admin_db.once('open', function callback(){
console.log("User Database connection open!\n");
});
var User_Schema = new mongoose.Schema({username: String, password: String});
var Admin = mongoose.model('Admin', User_Schema);
exports.manualLogin = function(user, pass, callback)
{
admin_db.find({username: user},function(err,docs){ //error is here
if(docs){
var x = 0;
var flag = false;
while(docs[x]){ //goes through all the admins
if (docs[x].param("username") == user){ //if it's a match
callback(null, docs);
flag = true;
break;
}
x+=1;
}
if (flag == false){
callback('invalid-password/username');
}
}
});
}
I get a TypeError that says:
Object #<NativeConnection> has no method 'find'
what's my problem?
I'm still quite new to node.js myself, but I'll try and answer anyway.
It looks like you've properly built your connection to mongodb through mongoose, but you have not created a schema. While mongodb doesn't have schemas, mongoose does.
What you'll need to do is create a schema (UserSchema) that matches the construction of a user document in your users collection, then create an instance of that schema (User) which is now your model, then call .find on that model.
The Mongoose Quick Start guide goes through this process:
http://mongoosejs.com/docs/index.html
EDIT after update:
You are currently calling admin_db.find. This does not exist. This is the error you are getting.
You need to change that to Admin.find. You also need to understand what the difference is.
EDIT once more:
You're not properly using the admin_db.once callback.
I suggest you go back and reread the Mongoose Quick Start guide I linked. It's quite short and goes through all of this.
I see you're rolling your own system here, but I thought I'd share a link to Drywall in case you could gain some inspiration or knowledge from it.
Drywall - A website and user system for Node.js: http://jedireza.github.io/drywall/
I hope this is helpful. Feel free to open issues in GitHub if you run into any road blocks.
I am working on a nodejs app.
Folder structure is
app
app.js
package.json
../model/schema.js
../controller/controller.js
../views
All the logic is in controller.js while app.js performing routing itself....
I want to know how to get/return data(object) from controller.js to app.js.
I am using 'return' to send mongodb document from controller to app but its undefined.
Heres code of app.js..i have removed unneccessary code
var express = require('express'),
app = express.createServer(express.logger()),
io = require('socket.io').listen(app),
routes = require('./routes');
var controller=require('./controller/controller');
var model=require('./model/schema');
app.get("/", function(req, res) {
res.render("chatroom.html");
});
io.sockets.on('connection', function (socket) {
socket.on('login',function(user)
{
var onliner=controller.loginUser(user);
console.log("Onlinersss: ",onliner);
});
socket.on('registerUser',function(user){
controller.registerUser(user);
});
});
Heres controller.js code:
var model=require('../model/schema');
exports.loginUser=function(user)
{
model.registerUser.findOne({uname:user.uname,pass:user.pass},function(err,doc){
if(!err){
console.log("Here loggedin: ",doc);
return doc;
}
else
console.log("Invalid username or password");
});
};
I've just pushed a project to GitHub that uses pretty much the same libraries and RethinkDB as the database (very similar to MongoDB). Basically you need to pass callbacks to your controller and have them invoked with the data retrieved from the DB. Take a look at app.js and lib/db.js in the linked project.
JavaScript (and therefore node.js) is asynchronous. When you 'return doc' the anonymous function defined at function(err, doc) is returned... not the function loginUser that you are trying to get data from.
loginUser returns immediately, and it returns undefined (since you don't specify anything else). To see what I mean, put 'return true;' as the last line in loginUser and you'll notice that you get the value back of 'true.'
However, you don't want to return a value. You want to callback with a value. That's beyond the scope of a stackoverflow answer, so here is a good resource:
http://bjouhier.wordpress.com/2011/01/09/asynchronous-javascript-the-tale-of-harry/