I am working on the user accounts part of my project. I successfully completed the the GET, POST and DELETE methods, now I am missing the PUT.
When the user submits the update user form, the req.body will be something like this
{ user:
{ firstName: 'asas',
lastName: 'assa',
studentId: '1234',
email: 'as#as.as',
password: '123'
}
}
My schema looks like this:
const userSchema = new Schema({
cuid: { type: 'String', required: true },
firstName: { type: 'String', required: true },
lastName: { type: 'String', required: true },
studentId: { type: 'Number', required: true },
password: { type: 'String', required: true },
email: { type: 'String', required: true },
dateAdded: { type: 'Date', default: Date.now, required: true },
lastLogin: { type: 'Date', default: null, required: false },
});
and finally my update function looks like this.
export function updateUser(req, res) {
console.log(req.body)
firstName = sanitizeHtml(req.body.user.firstName );
lastName = sanitizeHtml(req.body.user.lastName);
studentId = sanitizeHtml(req.body.user.studentId);
email = sanitizeHtml(req.body.user.email);
password = sha512(req.body.user.password).toString('hex');
let update = { firstName, lastName, studentId, email, password };
User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
if(error){
return res.status(500).send(err);
}else{
return res.json({ user: updated });
}
});
}
I can't figure out why my put method is not working, maybe a second pair of eyes can see the flaw.
You're close. The problem is that you aren't passing the id properly. MongoDB is looking for an identifier in the form of an object. You've written:
User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
What you need to do is separate your arguments into separate objects:
User.findOneAndUpdate({ _id: req.params.cuid }, { $set: update }, function(err, updated) {
Additionally, you need to use $set, otherwise you'll overwrite the whole Object. $set tells Mongo to only update the fields specified via the update object, which you defined a couple lines above.
you are not using the correct structure of findOneAndUpdate. first you have define , on which basis search the id.
export function updateUser(req, res) {
console.log(req.body)
firstName = sanitizeHtml(req.body.user.firstName );
lastName = sanitizeHtml(req.body.user.lastName);
studentId = sanitizeHtml(req.body.user.studentId);
email = sanitizeHtml(req.body.user.email);
password = sha512(req.body.user.password).toString('hex');
let update = { firstName, lastName, studentId, email, password };
User.findOneAndUpdate({_id: req.params.cuid}, {$set: updated}, function (err, user) {//correct structure
if(error){
return res.status(500).send(err);
}else{
return res.json({ user: updated });
}
});
}
hope this helps.
I believe you need to make the update parameter as an object.
The first parameter is a query object. For example {firstName : "whatever"}.
The second parameter contains the updates.
As an example, if you are trying to update an user last name, and searching it by the name, you should have something like
findOneAndUpdate({firstName:req.body.firstName},{lastName:req.body.lastName},function...})
I believe you are trying to search by the id, so you should put something like
findOneAndUpdate({cuid:req.body.cuid},{$set:{firstName:req.body.firstName,studentId:req.body.studentId...}),function...})
I hope my answer was helpful for you.
Related
I know populating schemas is not a new question but I am having a little trouble following the logic on this in regards to multiple schemas. I am working with
"mongoose": "^4.8.5",
"express": "^4.15.0",
I have a schema with a collection of caffeine drinks. When a user selects a drink i would like for that drink to be assigned to the user.
** If at any point I am missing something simple in the architecture please let me know. This project has been my intro to mongodb.
I am reading through populating on the mongoose documentation http://mongoosejs.com/docs/populate.html.
Essentially, if I am to assign the drinks to the list it looks like I want to add them as a reference in an array. This was my approach with caffeine_list
const SelectedDrinks = require('./userDrinks');
const UserSchema = mongoose.Schema({
name: {
type: String
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
caffeine_list: caffeine_list: [ // attempting to reference selected drinks
{
type: mongoose.Schema.Types.ObjectId,
ref: 'SelectedDrinks'
}
]
})
SelectedDrinks comes from the schema below. I added a reference to the user as the creator below
const User = require('./user');
let userDrinkSchema = new mongoose.Schema({
creator : {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
caffeine: Number,
mgFloz: Number,
name: String,
size: Number,
updated_at: {
type: Date,
default: Date.now()
}
});
This is where I start to get confused. I initially tried populate but could not get it going. If that was correct please let me know.
In regards to my task of adding a selected drink to the user I used addToSet. I was hoping that this would give me the drink info. I did my set up like so....
const User = require('../../models/user');
const UserDrinks = require('../../models/userDrinks');
router.post('/addDrink', (req, res, next) => {
let newDrink = new UserDrinks({
creator: req.body.creator,
caffeine: req.body.caffeine,
mgFloz: req.body.mgFloz,
name: req.body.name,
size: req.body.size,
updated_at: req.body.updated_at
});
newDrink.save( (err) => {
if(err) {
res.send(err);
} else {
User.findOne({ _id: newDrink.creator}, (err, user) => {
user.caffeine_list.addToSet(newDrink)
user.save( function (err) {
if(err) {
console.log(err);
}else {
res.status(201).json(newDrink);
}
})
})
}
})
});
However, after i do a post in postman I check caffeine_list and the result is
"caffeine_list" : [
ObjectId("58d82a5ff2f85e3f21822ab5"),
ObjectId("58d82c15bfdaf03f853f3864")
],
Ideally I would like to have an array of objects being passed with the caffeine info like so
"caffeine_list" : [
{
"creator": "58d6245cc02b0a0e6db8d257",
"caffeine": 412,
"mgFloz": 218.7,
"name": "1.95 Perfect Drink!",
"size": 42.93,
"updated_at": "2017-03-24T18:04:06.357Z"
}
]
Change your else part with below code instead of findOne and save use update
User.update(
{ _id: newDrink.creator},
{ $addToSet:{
caffeine_list: newDrink
}}).exec(function (err, updatedrink){
if(err) {
console.log(err);
}else {
res.status(201).json(updatedrink);
}
})
Although I am not sure this is the best approach I did find this to be give me the result that I was desiring. I had to make two small changes and I was able to get the caffeine_list to give me the desired response
I had to access the schema for selected drinks
const SelectedDrinks = require('./userDrinks').schema; //** need schema
Afterwards I was able to change
caffeine_list: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'UserDrinks' // name of the file
}
]
to
caffeine_list: [SelectedDrinks]
Now that I have the schema I am able to add the drinks directly into the caffeine_list on the UserSchema.
Hello so I am making a basic app with users and posts.
I followed the mongoose documentation on population (http://mongoosejs.com/docs/2.7.x/docs/populate.html) and setup my Schemas so that the users and be connected to posts
var userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
email: String,
created_at: Date,
updated_at: Date,
admin: Boolean,
posts: [{ type: mongoose.Schema.ObjectId, ref: 'Post' }]
});
var postSchema = new mongoose.Schema({
_user : [{ type: mongoose.Schema.ObjectId, ref: 'User' }],
audioFile: { type: String, required: true },
imageFile: { type: String },
title: { type: String, required: true },
artist: { type: String, required: true },
start: { type: String, required: true },
stop: { type: String, required: true },
genre: { type: String, required: true },
tags: [{ type: String }]
});
app.get('/', function (req, res){
Post.find({}, function(err, allPosts){
if(!err){
res.render('main.njk', {
posts : allPosts,
title : 'Title',
isLogged : req.session.isLogged,
user : req.session.user,
messages : req.flash('alert')
});
} else { return done(err); }
});
});
Thats all fine and gravy and I can run a foreach loop on allPosts to pull each one in my HTML, but when I try to think of how I am going to display all the posts with their respective users attached to each post I am unsure of how to connect the two since all the examples in the mongoose doc is just mainly for findOne.
I was thinking something like this
app.get('/', function (req, res){
Post.find({}, function(err, allPosts){
if(!err){
allPosts.populate('_user', ['username']);
allPosts.exec(function (err, users){
if(err) console.log(err);
console.log(users);
});
res.render('main.njk', {
posts : allPosts,
title : 'Spaurk.net',
isLogged : req.session.isLogged,
user : req.session.user,
messages : req.flash('alert')
});
} else { return done(err); }
});
});
but that doesn't work of course.
So I was wondering if anyone with experience with this situation would be able to help me solve this.
Thanks a lot for any input.
EDIT, thanks to Daves help I was able to get the populate to work properly, I just cant pull the fields I want correctly with
Post.find({}).populate('_user').exec(function(err, allPosts){
In my loop {% for post in posts %}
, when I do post._user it shows the whole user schema, but when I do post._user.username it doesn't return anything. I am unsure as to why this is.
The proper way to structure a populate on a query is like this:
Post.find({})
.populate('_user')
.exec((err, allposts){...})
Then you will have an array of your Posts with the _user array populated. If you need to access a property of a user, you will need to do another loop through the _user array or specify with use you want to use _user[0].<property>
I have an User model defined. This model has two lists, one of the items the user has liked and the other one is of the items the user has disliked.
I need a list of the items that one User hasn't qualified (neither liked nor disliked) and the other users did. I'm using the Mongoose library for NodeJS and also the lodash(_) library,
my code looks like this:
function itemsUserHasntQualified(var user){
items = [];
User.find().exec(function(err, users){
for(var user_it: users){
if(user_it != user){
items.push(_.difference(user_it.tracks.liked, user_it.tracks.disliked, user.tracks.liked, user.tracks.disliked);
}
}
});
}
This is the schema for the User:
var UserSchema = new Schema({
name: String,
username: {type: String, lowercase:true },
email: { type: String, lowercase: true },
role: {
type: String,
default: 'user'
},
hashedPassword: String,
provider: String,
salt: String,
facebook: {},
twitter: {},
google: {},
github: {},
tracks: {
liked: [{type:Schema.ObjectId, ref: "Track"}],
disliked: [{type:Schema.ObjectId, ref: "Track"}],
later: [{type:Schema.ObjectId, ref: "Track"}]
}
});
But actually I'm feeling this is not the correct way of do it.
Is there a simpler or more correct way of query this?
I'm not sure what classifies as correct, but you can at least run the bulk of the query in mongodb without returning the entire collection.
Mongoose Query#distinct which is db.collection.distinct() will return distinct array items and can be supplied a query.
User.distinct('tracks.disliked', { username: { $ne: username } })
User.distinct('tracks.liked', { username: { $ne: username } })
This will give you the arrays for liked and disliked, which you can then difference for a user.
UserSchema.methods.itemsUserHasntQualified = function () {
var user = this
var liked = User.distinct('tracks.liked', { username: { $ne: user.username } })
var disliked = User.distinct('tracks.disliked', { username: { $ne: user.username } })
Promise.all([liked, disliked]).then(function (results) {
var all_ratings = _.union( results[0], results[1] )
var users_ratings = _.union( user.tracks.liked, user.tracks.disliked )
var missing = _.difference( users_ratings, all_ratings )
return missing
})
}
Depending on your access patterns, you might want to run this collection scan somewhere else, less frequently and cache the array results for use in itemsUserHasntQualified.
I've created a Mongoose Schema that looks like this:
var UserSchema = new Schema({
user: [{
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
index: { unique: true }
},
password: {
type: String,
required: true
},
mobile: Number
}],
account: [{
locked: {
type: Boolean,
default: false
},
accountType: {
type: String,
default: "guest"
},
failedLogins: {
type: Number,
default: 0
}
}],
reset: [{
resetToken: String,
resetExpirey: Date
}],
details: [{
accountCreated: Date,
lastLogin: Date
}]
});
As you can see I've tried to group certain fields. Is this the correct way to do it? I'm now having trouble referencing the fields. I get an error when I try this:
User.create({
user.name : req.body.name,
user.email : req.body.email,
user.password: req.body.password
}, function(err) {
if (err) res.send(err);
});
Error is unexpected token '.' in user.name
Your create statement still needs to be proper javascript, so your left-side object literals will need to be strings.
User.create({
'user.name' : req.body.name,
'user.email' : req.body.email,
'user.password': req.body.password
}, function(err) {
if (err) res.send(err);
});
Furthermore, because Mongoose requires you to have an array instead of allowing proper sub-objects, you'll need to actually insert these as an array.
var user = {user: [{name:req.body.name, email: req.body.email, password: req.body.password}]};
User.create(user) ...
As to the "is it worth it" to do it like this, my opinion is: no. Just put all these things in the root object unless you plan on having more than one user or more than one account in this one document.
I have two Schemas:
var ProgramSchema = new Schema({
active: Boolean,
name: String,
...
});
var UserSchema = new Schema({
username: String,
email: { type: String, lowercase: true },
...
partnerships: [{
program: { type: Schema.Types.ObjectId, ref: 'Program' },
status: { type: Number, default: 0 },
log: [{
status: { type: Number },
time: { type: Date, default: Date.now() },
comment: { type: String },
user: { type: Schema.Types.ObjectId, ref: 'User' }
}]
}]
});
Now I want to get all Program docs, but also append 'status' to each doc, to return if the program is already in a partnership with the logged in user.
My solution looks like this:
Program.find({active: true}, 'name owner image user.payments', function (err, p) {
if(err) { return handleError(res, err); }
})
.sort({_id: -1})
.exec(function(err, programs){
if(err) { return handleError(res, err); }
programs = _.map(programs, function(program){
var partner = _.find(req.user.partnerships, { program: program._id });
var status = 0;
if(partner){
status = partner.status;
}
program['partnership'] = status;
return program;
});
res.json(200, programs);
});
The req.user object contains all information about the logged in user, including the partnerships array.
To get this solution to work, I have to append
partnership: Schema.Types.Mixed
to the ProgramSchema.
This looks a bit messy and thats why I am asking for help. What do you think?
When you want to freely modify the result of a Mongoose query, add lean() to the query chain so that the docs (programs in this case) are plain JavaScript objects instead of Mongoose doc instances.
Program.find({active: true}, 'name owner image user.payments')
.lean() // <= Here
.sort({_id: -1})
.exec(function(err, programs){ ...
Then you can remove partnership from your schema definition. Your query will also execute faster.