Insert embedded documents Mongo DB from express app - javascript

I have a mongo db schema like so
users:
{username:"",
age: "",
data: [
{field1:"", field2:""},
{field1:"", field2:""}
]
}
My problem is how do send over my user object to my express route to post data to the db.
Typically i create my user object like so:
var user= {username:"test" age: 20};
and send this user object with my ajax call to my adduser route.
Ho do i structure my above assignment to include my embedded documents.
when i use this structure:
sign_in_data: [{date:"",time_in:"",time_out:""}]
my database looks like this:
sign_in_data[0][date]: "",
sign_in_data[0][time_in]: "",
sign_in_data[0][time_out]: ""
but it should look like this:
sign_in_data: [
{
date: "2015-06-08",
time_in: "17:35",
time_out: "17:35"
},
]

at the moment when you are configuring express, review if you are using this line of code:
In case you are using Express 4.x version
Note: you need to have body-parser installed.
app.use(bodyParser.urlencoded({ extended: true }));
In case you are using Express 3.x version
app.use(express.urlencoded());
I did a test example where the property cars is an an array of objects send from the html file, and it is saving as you want in the database.
So this is my express server file:
// ..
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/user', function (req, res) {
var user = {
username: req.body.username,
age: req.body.age,
cars: req.body.cars
};
db
.collection('user')
.insert(user, function (err, result) {
if (err) return res.status(500).send(err);
res.send(result);
});
});
// ..
database data inserted:
{
"_id" : ObjectId("5578b48f1e8cdf0520bdf76f"),
"username" : "wilsonbalderrama",
"age" : "29",
"cars" : [
{
"name" : "Ferrari",
"color" : "Red"
},
{
"name" : "Mustang",
"color" : "Gray"
}
]
}
and my html file where I am sending the data, you can review it in this gist file:
https://gist.github.com/wilsonbalderrama/62f76a348c0aa217cdd9
Hope this helps you.

Related

body-parser is not parsing json data

This is how I am using the package:
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
This is the body of my request:
{
name: "jay",
price: "12"
}
This is how I am extracting the body:
const name = req.body.name;
const price = req.body.price;
But both name and price returned undefined.
EDITED:
According to VS Code, this package is deprecated. But it should still work, no?
the request body should be JSON so property names should be in quote signs
{
"name": "jay",
"price": "12"
}

When post to express server after use JSON.stringify, it doesn't work

I am trying to send a simple array to express server as JSON, then convert it back to an object on the server side.
Client side :
var hand = { cards: [] }
// I randomly generate some numbers and suits and then add to the array.
var card_obj = { "number": number, "suit": suit };
hand.cards.push(card_obj)
//Send to Express server
var hand_json = JSON.stringify(hand)
$.post("hand", hand_json, function(result) {
console.log(result);
});
Server side :
app.post("/hand", function(req, res) {
var cards = req.body.cards
console.log(cards[0])
});
This code is not working,as I receive on the server side console : Cannot read property '0' of undefined
BUT, it will work, If i changed the client code to the following :
var test = { "cards": [{ "number": "9", "suit": "club" }, { "number": "10", "suit": "club" }, { "number": "K", "suit": "spades" }, { "number": "A", "suit": "hearts" }, { "number": "5", "suit": "diamonds" }] }
$.post("hand", test , function(result) {
console.log(result);
});
The weird thing that the test variable is the same variable generated by JSON.stringify(hand), I just copied it form the console.
I don't understand why when use JSON.stringify(hand), it doesn't work. But when copy paste the object and then pass it, it works.
In order to send a post request content JSON in the body you need to make sure two things:
Since you are using JQuery. From client, you must use ajax function to include a contentType:"application/json in your request to inform the server you are sending a JSON file:
$.ajax({
url: url,
type:"POST",
data: JSON.stringify(hand),
contentType:"application/json",
dataType:"json",
success: function(result){
`...Do something when the data returned`
}
})
Now in the server, you should install and define a middleware to work as body parser for your post requests
Install npm install body-parser --save
And use it in express
const bodyParser = require('body-parser');
// support parsing of application/json type post data
app.use(bodyParser.json());
//And make sure your route is placed below the parser
app.post("/hand", function(req, res) {
var cards = req.body.cards
console.log(cards[0])
});

mongodb array data store using node js

I have to try to store my child info into MongoDB via using postman tool. But it shows me this message "message": "child info validation failed"
in postman console. child info is my collection name where I store my child info.
my requirement is to store the result in array form as below schema mentioned inside MongoDB
1). This is js child schema
userId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
quiz:[
{
questionId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'questions'
},
score:{type:String},
time:{type:String}
}
]
2). This is node js
try {
var quizArr = [];
var quizObj = {
'questionId': req.params.questionId,
'score': req.params.score,
'time': new Date().toISOString()
};
quizArr.push(quizObj);
var userObj = {
'userid': req.params.userId,
'quiz': quizArr
};
//var parseObj = Json.stringify(userObj); This line is comment
var childinfoSave = new QuizChildInfo(userObj);
childinfoSave.save(function (err) {
if (err) return next(err);
res.send("Child questionId score and date saved successfully")
console.log("Child questionId score and date saved successfully");
});
}catch(err){console.log(err);}
3). Output of postman screen
{
"message": "childinfos validation failed"
}
4). Output of console
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
5). Mongo console
{
"_id" : ObjectId("57bc483169e718642ac0ac44"),
"levelsAttempted" : [ ],
"quiz" : [ ],
"__v" : 0
}
For the problem of your console,
put mongoose.Promise = global.Promise; in the file where you have established your server connection( like in my case its index.js).
And i think you may have not posted the whole code as i couldnt find childinfos in your code.

How to manipulate data returned from mongo db using mongoose

I've created a local database using mongo (using this tutorial actually)
It has a db named 'simple' and collection named 'people'. Then I import json with each element as
{
"id": 1,
"guid": "1581cfde-f2fc-44f8-8953-511331e943ab",
"isActive": true,
"firstName": "Ilene",
"lastName": "Kent",
"email": "carolegoodman#intrawear.com"
}
I then create the schema and Person model in my node app
var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var app = express();
app.set('port', (process.env.PORT || 5000));
mongoose.connect('mongodb://localhost/simple')
var personSchema = {
firstname: String,
lastname: String,
email: String
}
var Person = mongoose.model('Person', personSchema, 'people')
app.get('/users', function(req,res){
Person.find(function(err, doc){
var x = doc[0]
console.log(x)
console.log(Object.keys(x))
res.send(200);
});
});
On calling find() on the Person model I get logged (for console.log(doc[0])) - the first item in the doc returned:
{ _id: 548e41afa0bad91d53f34cce,
id: 0,
guid: 'af6a931d-1801-4662-9d52-c95dc97bac22',
isActive: false,
firstName: 'Janna',
lastName: 'Shelton',
email: 'crossoconnor#geekology.com' }
But my problem is that when I look for the property firstName on doc[0] (i.e. doc[0].firstName) I get an undefined.
I've tried diagnosing this and Object.keys(doc[0]) gives me:
[ '$__',
'isNew',
'errors',
'_maxListeners',
'_doc',
'_pres',
'_posts',
'save',
'_events' ]
meaning I suspect there must be some special methods for mongoose when you want to access the data from your returned elements - but I can't find the answer in documentation or here.
Thanks
You receive an array of Documents. Mongoose API
doc[0].get('firstName')
When you just want a plain JavaScript representation of the documents that you can freely manipulate, add lean() to your Mongoose query chain:
app.get('/users', function(req,res){
Person.find().lean().exec(function(err, docs){
var x = docs[0]
console.log(x)
console.log(Object.keys(x))
res.send(200);
});
});
Use .lean() in your query as below.
db.collection.find().lean().then(function(data){})

How to display the results of an aggregation in jade

How do you display the results of a mongodb aggregation query using node.js, express & jade
I'm not sure what I've done wrong or even if this is the correct approach.
When I attempt to access the page - it just times out?
Any idea? Or is there a better way?
app.js
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/soundevents');
var timeroute = require('./routes/timers');
app.get('/time/starttimer', timeroute.starttimer);
my routes/timers.js looks like this:
exports.starttimer = function(db) {
return function(req, res) {
var aEvent = db.get('event');
aEvent.aggregation([{$group:{_id:{"EventName":"$EventName"}}}],{}, function(e,docs) {
res.render('time/starttimer', {title: 'Stat Timer',
"eventlist" : docs});
});
};
};
output from mongo
db.event.aggregate([{$group:{_id:{"EventName":"$EventName"}, UpdateTime:{$max: "$UpdateTime"}}}])
{"result" : [{"_id" : {
"EventName" : "MtnBikeRace 1"},
"UpdateTime" : 1392265180.069293},
{"_id" : {
"EventName" : "Student League"},
"UpdateTime" : 1392047321724}],
"ok" : 1}
This example can help you.
https://github.com/visionmedia/express/blob/master/examples/jade/index.js
// Dummy users
var users = [
new User('tj', 'tj#vision-media.ca')
, new User('ciaran', 'ciaranj#gmail.com')
, new User('aaron', 'aaron.heckmann+github#gmail.com')
];
app.get('/', function(req, res){
res.render('users', { users: users });
})
// user.jade
extends ../layout
block content
h1 Users
#users
for user in users
.user
h2= user.name
.email= user.email
I landed here because I was looking for a way to aggregate results using monk.
For future reference: as of now, monk has no .aggregate() method, therefore this part of your code will not work (as you've indicated in a comment):
// there's no aggregation method.
aEvent.aggregation([{$group:{_id:{"EventName":"$EventName"}}}],{}, function(e,docs) {
res.render('time/starttimer', {title: 'Stat Timer',
"eventlist" : docs});
});
However,
this answer shows the correct way to perform an aggregation with monk. It uses the underlying native driver. In your case this would be something like the following:
aEvent.col.aggregate([{$group:{_id:{"EventName":"$EventName"}}}],{}, function(e,docs) {
res.render('time/starttimer', {title: 'Stat Timer',
"eventlist" : docs});
});
If you aggregate this way, I guess you can accept fundon's answer.

Categories