ExpressJS - can't get data on update User - javascript

I want to create an experience array in User model with new data, and the problem is that I don't get saved data in exec function so I can push new data in array on frontend. This is what I got so far.
router.post('/:username/experience', function(req, res) {
const username = req.params.username;
User.findOneAndUpdate(
username, {
$push: {
experience: req.body
}
}, {
safe: true,
upsert: true
})
.exec(function (err, data) {
console.log(data, "------>");
});
})
This is my schema for experience, which is called in User model like experience: [ExperienceSchema].
const ExperienceSchema = new Schema({
title: {
type: String,
required: true
},
company: {
type: String,
required: true
},
from: {
type: Date,
},
to: {
type: Date,
},
workingNow: {
type: Boolean,
default: false
},
description: {
type: String
}
}, {
usePushEach: true
})

Since findOneAndUpdate returns the original document (state before update) you need to add new: true to the options in order to get the updated document.
options:
{
safe: true,
upsert: true,
new: true
}

Related

Mongoose Virtual reference between two ObjectIds

I know it is possible to create a virtual reference using a local _id to a foreign ObjectId, but is it possible to use a local ObjectId to a foreign ObjectId?
I'm trying to figure out if I have a bug somewhere or if this is not possible because it's not working.
const Post = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
text: {
type: String,
}
})
const ProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
bio: {
type: String,
max: 500,
}
});
ProfileSchema.virtual('posts', {
ref: 'Post',
localField: 'user',
foreignField: 'user',
});
// query
const profile = await Profile.findOne({
user: req.user.id,
}).populate('posts')
Yes it is possible, but you need to add { toJSON: { virtuals: true } } to the Schema.
const ProfileSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
bio: {
type: String,
max: 500,
},
},
{
toJSON: { virtuals: true },
}
);
From docs:
Keep in mind that virtuals are not included in toJSON() output by default. If you want populate virtuals to show up when using functions that rely on JSON.stringify(), like Express' res.json() function, set the virtuals: true option on your schema's toJSON options.
https://mongoosejs.com/docs/populate.html#populate-virtuals

Validation for findOneAndUpdate doesn't have access to "this" values

I've got an Express endpoint that does the following:
router.put('/:appointmentId', async (req, res) => {
try {
let appointment = await Appointment.findOneAndUpdate(
{ _id: req.params.appointmentId },
{
member_id: req.body.appointment.memberId,
client_id: req.body.appointment.clientId,
address_id: req.body.appointment.addressId,
unit_id: req.body.appointment.unitId,
dateAndTime: req.body.appointment.dateAndTime
},
{
new: true,
runValidators: true,
context: 'query'
}
);
res.send(appointment);
} catch (err) {
res.send(err);
}
});
Appointment is a Mongoose model. This model has one of the following path validations:
AppointmentSchema.path("unit_id").validate(async function (unit_id) {
let unit = await Unit.findById(unit_id);
if ((unit === null) || !unit.address_id.equals(this.address_id)) {
return false;
}
return true;
}, "unit_id is not valid");
The Appointment schema looks like this:
var AppointmentSchema = new mongoose.Schema(
{
member_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Member',
required: true
},
client_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Client',
required: true
},
address_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Address',
required: true
},
unit_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Unit',
required: true
},
dateAndTime: {
type: Date,
required: true
}
}
);
When I hit the Express endpoint, this.address_id is undefined and my validations don't work properly. It's my understanding that if I set runValidators to true and context to 'query' that I'd have this access.
What am I doing wrong?
As a side-note, I also notice that Express is returning a 200 status when validation fails. This is curious.
Thanks.

Model methods are not working in sails js

I am using sails v0.12 , I have different models in my MySql relational database but the area of concern lies in these 2 models which are
1. User
2. Appointment
I want to add apptCustomer and apptProvider data into appointment model
but the value is NULL in the database
My User model is :
module.exports = {
attributes: {
name: {
type: "string",
},
email: {
type: "email",
required: true,
unique: true
},
contact_no:{
type: "int",
maxLength: 15,
//required: true,
},
address: {
type:"longtext",
//required: true,
},
userId:{
type: "string",
primaryKey: true,
unique:true
},
gtoken:{
type: "string"
},
provider:{
type:"string"
},
cancel:{
type: "boolean",
// required: true
},
business_name:{
type:"string",
unique:true
},
business_category:{
type:"string"
},
roles:{ // Many to Many = User <-> User-role <-> Role
collection:'Role',
through:'userrole',
},
services:{
collection:'Service',
through:'serviceprovider', // Many to Many = User (provider) <-> Service-provider <-> Service
},
schedules:{ // One to One = User (provider) - Schedule
collection:'Schedule',
via:'owner',
},
providerAppointments:{ // One to Many = User(customer) - multiple Appointments
collection:'Appointment',
via:'appProvider',
},
customerAppointments:{
collection:'Appointment', // One to Many = User(provider) - multiple Appointments
via:'appCustomer'
}
}
};
And my Appointment Model is
module.exports = {
attributes: {
appointment_id:{
type:'int',
primaryKey: true,
autoIncrement:true
},
appointmentDate: {
type: 'datetime',
required: true,
},
start_time: {
type: 'string',
required: true,
},
end_time: {
type: 'string',
},
status: {
type: 'string',
enum: ['booked', 'canceled']
},
appProvider: {
model: 'user',
},
appCustomer:{
model: 'user',
},
serviceAppointment: {
model: 'service',
}
}
};
And my Model Methods are as follows
Appointment.findOrCreate({appointmentDate:req.body.values.appointmentDate, start_time:req.body.values.start_time, end_time:req.body.end_time, status: status},{appointmentDate:req.body.values.appointmentDate, start_time:req.body.values.start_time, end_time:req.body.end_time, status: status})
.exec(function apptCreated(err,appt){
if(err) { sails.log('err',err)}
Service.findOne({ service_id : req.body.values.selected_service})
.exec(function(err,service){
service.serviceAppointments.add(appt);
service.save(function(err,result){
if(err) { sails.log(err)}
})
}),
User.find({userId: req.body.businessId})
.populate('roles')
.exec(function(err,provider){
_.map( provider.roles, role => { // Problem lies here .. this method is not working
if(role.role_id==1){
provider.providerAppointments.add(appt);
provider.save(function(err, result){
if(err) { sails.log(err)}
sails.log('appointment added to provider')
})
}
})
}),
//Appointment adding to Customer
User.find({userId: req.body.customerId})
.populate('roles')
.exec(function(err,customer){
_.map( customer.roles, role => { // Problem lies here... this method is not working
if(role.role_id==2){
customer.customerAppointments.add(appt)
customer.save(function(err, result){
if(err) { sails.log(err)}
sails.log('appointment added to customer')
})
}
})
}),
// Adding contact to customer
User.update({userId: req.body.customerId},{ contact_no: req.body.values.contact_no}) // this method works fine
.exec(function userUpdated(err, user){
if(err) { return sails.log(err)}
sails.log('contact number updated',user);
})
})
As far as I can see, at the moment that you call provider.providerAppointments.add, provider.providerAppointments is still just a regular property - possibly an array of ids.
I think you need to add .populate('providerAppointments') to your User.find... if you do that, then provider.providerAppointments should have a .add method that works the way you expect.
Of course, if this is the source of error, I would have expected a pretty clear error message, like provider.providerAppointments.add is not a function or some such. But try adding the populate, see if it fixes your problem.

id: null issue in sailsJS when create an User

I suspect my issue is something silly, but I can't find the error. I'm using mongodb to persist user data. Every seems to work fine but when I look the database I have id: null in every user record.
I don't want that id: null, I already have _id field.
$sails --version
0.11.0
My userController:
module.exports = {
create: function(req, res) {
User.create(req.params.all(), function userCreated(err, user) {
if(err) res.json(401, err);
res.json(200, user);
})
}
}
My user model:
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
email: true,
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
maxLength: 15,
columnName: 'encrypted_password',
required: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
},
beforeCreate: function(values, next) {
require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
if(err) console.log(err);
values.password = encryptedPassword;
next();
});
}
};
When I create a user from url like http://localhost:1337/user/create?name=theUser&email=user#mail.com&password=123456&role=admin everythink seems to be fine but in my mongodb I see this: (id: null)
{
name: "theUser",
email: "user#mail.com",
role: "admin",
id: null,
createdAt: ISODate("2015-04-27T18:34:42.678Z"),
updatedAt: ISODate("2015-04-27T18:34:42.678Z"),
encrypted_password: "$2a$10$iNt/OR8XhjijqRjkpoNW/eR70HTSDgVJ2WmNppqab79rZt213aywm",
_id: ObjectId("553e81429255e51f419a8ffc")
}
I'd tried with autoPK: false but nothing happens.
Thanks in advance.
As stated in waterline docs,
By default an attribute named id will be automatically added to your
model which will contain an auto-incrementing number unique to each
record. This will be your model's primary key and will be indexed when
available. You can override this if you would like to define your own
primary key factory or attribute.
primaryKey
Will set the primary key of the record. This should be used when autoPK is set to false.
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
}
}

Manipulating Mongoose/MongoDB Array using Node.js

I've noticed there's little documentation and info about how I should manipulate an array of objects using Mongoosejs.
I have the following model/Schema for an User:
'use strict';
/**
* Module Dependencies
*/
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var mongoose = require('mongoose');
/**
* Custom types
*/
var ObjectId = mongoose.Schema.Types.ObjectId;
var userSchema = new mongoose.Schema({
email: { type: String, unique: true, index: true },
password: { type: String },
type: { type: String, default: 'user' },
facebook: { type: String, unique: true, sparse: true },
twitter: { type: String, unique: true, sparse: true },
google: { type: String, unique: true, sparse: true },
github: { type: String, unique: true, sparse: true },
tokens: Array,
profile: {
name: { type: String, default: '' },
gender: { type: String, default: '' },
location: { type: String, default: '' },
website: { type: String, default: '' },
picture: { type: String, default: '' },
phone: {
work: { type: String, default: '' },
home: { type: String, default: '' },
mobile: { type: String, default: '' }
}
},
activity: {
date_established: { type: Date, default: Date.now },
last_logon: { type: Date, default: Date.now },
last_updated: { type: Date }
},
resetPasswordToken: { type: String },
resetPasswordExpires: { type: Date },
verified: { type: Boolean, default: true },
verifyToken: { type: String },
enhancedSecurity: {
enabled: { type: Boolean, default: false },
type: { type: String }, // sms or totp
token: { type: String },
period: { type: Number },
sms: { type: String },
smsExpires: { type: Date }
},
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
});
/* (...) some functions that aren't necessary to be shown here */
module.exports = mongoose.model('User', userSchema);
So as you can check I defined Friends inside User like this:
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
Now the question is how can I add, edit and delete this array in a Node.js script?
BOTTOMLINE: How can I manipulate arrays that are inside MongoDB Schemas, using Node.js and Mongoose.js? Do I always have to create a Schema function or can I access it directly?
EDIT (13/07/2014): So far I've created a HTTP GET that gives me the array like this:
app.get('/workspace/friends/:userid', passportConf.isAuthenticated, function (req, res) {
User.find({_id: req.params.userid}, function (err, items) {
if (err) {
return (err, null);
}
console.log(items[0].friends);
res.json(items[0].friends);
});
});
But this only returns an array of friendIds, but what if I want to create some sort of '/workspace/friends/:userid/del/:friendid' POST, or add POST. I can't seem to figure out how I can get this done.
You can do something like following
app.get('/workspace/friends/:userid/delete/:friendId', passportConf.isAuthenticated, function (req, res) {
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
for (var i = 0; i < user.friends.length; i++) {
if (user.friends[i]._id === req.params.friendId) {
user.friends = user.friends.splice(i,1)
}
}
user.save(function(err, user, numAffected){
if (!err )res.json(user)
res.send('error, couldn\'t save: %s', err)
})
});
});
What it says in mongoose docs is that
"The callback will receive three parameters, err if an error occurred, [model] which is the saved [model], and numberAffected which will be 1 when the document was found and updated in the database, otherwise 0.
The fn callback is optional. If no fn is passed and validation fails, the validation error will be emitted on the connection used to create this model."
If you need to manipulate arrays, you should convert these in objects before.
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
var user = user.toObject();
//... your code, an example =>
delete user.friends;
res.json(user);
});
Regards, Nicholls

Categories

Resources