Mongoose do not validate on update - node.js

I currently have a Mongoose Schema with validation:
assignmentID: {
type: Number,
validate: {
validator: function(v) {
if (!v) {
return false;
}
return Assignment.findById(v)
.then(assignmentDoc => {
if (!assignmentDoc) {
return false;
} else {
return true;
}
})
.catch(error => {
return false;
});
},
message: "Invalid assignment ID."
}
}
The validator works perfectly. However, it is executed when I update a document via .save().
Is it possible to change it so the validation executes only when the document is created and not when the document is updated?

Yes, you can set validateBeforeSave to false:
var schema = new Schema({ name: String });
schema.set('validateBeforeSave', false);
schema.path('name').validate(function (value) {
return v != null;
});
var M = mongoose.model('Person', schema);
var m = new M({ name: null });
m.validate(function(err) {
console.log(err); // Will tell you that null is not allowed.
});
m.save(); // Succeeds despite being invalid
reference: https://mongoosejs.com/docs/guide.html#validateBeforeSave

Related

Can't save Data in Json Format in Mongodb

In this application, I am saving semester_id, course_id and Subject in Mongodb. All I need is to save the Subject in Json format. I want to save semester_id , course_id and save Subject in Json(not in array) with same ids - For semeter and course. I am saving subject in array and I am new to Angular. Can anyone help me out. Thanks in advance.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SubjectSchema = new Schema({
semesterId:{type: String,ref:'semesterNew'},
courseId :{type: String,ref:'CollegeCourse'},
subject:{
subject :{ type:String}
},
createdOn : {type:Date,default:Date.now},
updatedOn : {type:Date,default:Date.now},
});
mongoose.model('Subject',SubjectSchema);
router.post('/addSubject',function(req,res){
var subjects = JSON.stringify(req.body.subject);
var subjectData = new subjectModel({
semesterId:req.body.semesterId,
courseId: req.body.courseId,
subject: subjects,
});
subjectData.save(function (err, result) {
if (err) {
console.error(err);
return res.status(400).json({
message: 'Bad Request'
});
} else {
res.json({
status: 200,
data: result
})
console.log('Check',result);
}
});
});
addSubject(item){
return this.api.post(`${this.apiController}/addSubject`,item);
}
saveSubject() {
const config = {
position: NbGlobalPhysicalPosition.BOTTOM_RIGHT
};
const formData = new FormData();
this.subjectMasterForm.controls.semesterCtrl.markAsDirty();
this.subjectMasterForm.controls.collegeCourseCtrl.markAsDirty();
// this.subjectMasterForm.controls.image.markAsDirty();
var all_subject_array = [];
if (this.subjectMasterForm.valid && this.subjectMasterForm.value.subjects.length > 0) {
if (this.semesterSubjectId == '' || this.semesterSubjectId == null || this.semesterSubjectId == 'undefined' || this.semesterSubjectId== undefined) {
var subjects_values = this.subjectMasterForm.value.subjects
var subjects_length = this.subjectMasterForm.value.subjects.length;
subjects_values.forEach(function (element) {
all_subject_array.push(element.name);
console.log('Check2',element.name);
});
this.overview_data = {
courseId: this.courseId,
semesterId:this.semesterId,
subject: all_subject_array,
semesterSubjectId: this.semesterSubjectId,
}
this.collegeTemplateApi.addSubject(this.overview_data).subscribe(data => {
if (data['status'] == 200) {
this.toasterService.show("Subject successfully Added!!!..", `Success`, config);
} else {
this.toasterService.show("Subject Already exists in our Database!!!...", `Success`, config)
}
});
} else {
if(this.courseId!=undefined && this.semesterId!=undefined){
if (this.subjectMasterForm.value.subjects.length > 0) {
var subjects_values = this.subjectMasterForm.value.subjects
var subjects_length = this.subjectMasterForm.value.subjects.length;
subjects_values.forEach(function (element) {
all_subject_array.push(element.name);
});
this.overview_data = {
courseId: this.courseId,
semesterId:this.semesterId,
subject: all_subject_array,
semesterSubjectId: this.semesterSubjectId
}
}
this.collegeTemplateApi.updateSubject(this.overview_data).subscribe(data => {
if (data['status'] == 200) {
this.toasterService.show("Subject successfully Updated!!!..", `Success`, config);
} else {
this.toasterService.show(data['message'], `Success`, config)
}
});
}
}
} else if (this.subjectMasterForm.value.subjects.length == 0) {
this.subjecterror = true;
}
setTimeout(() => this.ngOnInit(), 3000);
}
In your first code segment, the Subject model is not assigned to a variable.
mongoose.model('Subject',SubjectSchema);
Yet later in your code you declare a new instance of subjectModel.
Try assigning the model to this name.
var subjectModel = mongoose.model('Subject', SubjectSchema);

Firebase function returning null value when called from iOS app

I am making a call to a callable firebase function from my iOS app and am getting a return value of null. The correct value was returned just a few days ago, but now it's always returning null. The data is logging correctly in the console just before the return line, and there is no error appearing within the iOS call.
exports.startPlaylist = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
const signature = data.signature;
return axios.post('---url----', {
data: signature
}).then(function(response) {
const val = response.data;
const ref = database.ref().push();
ref.set({
host: {
uid: uid
},
users: {
uid: uid
},
books: val
}, function(error) {
if(error) {
console.log('Not set');
} else {
const info = { id: ref.key };
console.log(info) //Correct log value appears in console
return info; //Return null, however
}
});
}).catch(function(err) {
console.log(err);
});
});
Firebase call
Functions.functions().httpsCallable("startPlaylist").call(["signature": signature]) { (result, error) in
guard let result = result, error == nil else { return }
print(result.data) //<-- prints "null"
}
As stated you should work with the promise returned by set:
ref.set({
host: {
uid: uid
},
users: {
uid: uid
},
books: val
}).then(function() {
const info = { id: ref.key };
console.log(info)
return info;
})
.catch(function(error) {
console.log('Not set');
});

Sequelize how to get all data if there is no query string passed

I'm pretty new to Sequelize.
I'm trying to create a handler to get all playlists in my database.
This is what I want to do:
If there is a query string then it should return the result based on that query.
If there is no query string passed then it should return all my playlists.
This is my playlist model:
const Playlist = db.define("playlist", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: Sequelize.STRING,
unique: true,
},
});
Here is my handler:
exports.getPlaylists = async (req, res) => {
try {
const { name } = req.query;
console.log(name); // this prints the name
const result = await Playlist.findAll({
where: {
name:
name === undefined ? {} : { $like: `%${name}%` },
},
});
if (result) {
res.status(200).send(result);
} else {
response.status(404).send("No Playlists found");
}
} catch (error) {
res.status(500).send(`Internal server error: ${error}`);
}
};
This works well if I passed a name in the query. but If I didn't pass any query string. It returns an empty array.
$like is an alias for Sequelize.Op.like
What should I put instead of the empty object?
I checked this question How get all data if field in query string is empty Node and Sequelize with Postgres but the proposed solutions didn't work with me
Create a filter object based on the condition. If you pass empty object to where, it won't apply that in the query.
exports.getPlaylists = async (req, res) => {
try {
const { name } = req.query;
const filters = {};
if (name)
filters.name = {
[Op.like]: `${name}%`, // you can also use $like if you are using older version of sequelize
}
const result = await Playlist.findAll({
where: filters,
});
if (result) {
res.status(200).send(result);
} else {
response.status(404).send("No Playlists found");
}
} catch (error) {
res.status(500).send(`Internal server error: ${error}`);
}
};
This way you can prepare complex filters based on other query strings.

Nodejs findOneAndUpdate paramerters

I have a user model with the following schema, all fields required
name:String,
country:String,
age:Number
From frontend i press update button and update only the age field, and on backend i have the following
var body = _.pick(req.body, ["candidateId","name","age","country"]);
var candidateId = mongoose.Types.ObjectId(body.candidateId);
Candidate.findOneAndUpdate({
_id:candidateId
},{
name: body.name,
country: body.country,
age: body.age,
}).then((data) => {
if (!data) {
res.status(400).send('noDataFound');
} else {
res.status(200).send(data);
}
}).catch((e) => {
res.status(500).send(e)
})
Front passes the data as follows
{ "candidateId":"5a9a86c16acff45a8070d2da",
"name":"Superman",
}
I will get get undefined error for body.age and body.country since its not passed from front end.
QUESTION - How can use the previous values if no value is send from front end for some parameter, one approach is to send everything even if its not changed.
It looks like you're using lodash, check out omitBy to remove undefined or null properties:
_.omitBy({ foo: 'foo', bar: undefined, baz: null }, _.isNil)
{foo: "foo"}
Here's a vanilla JS solution:
Object
.entries({ foo: 'foo', bar: undefined, baz: null })
.filter(([_, value]) => value !== null && value !== undefined)
.reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {})
When I'm facing this problem I usually create a parser function as:
var body = _.pick(req.body, ["candidateId","name","age","country"]);
var candidateId = mongoose.Types.ObjectId(body.candidateId);
function createUpdatedValues(params) {
var condition = {};
if (params.candidateId) condition.candidateId = params.candidateId;
if (params.name) condition.name = params.name;
if (params.age) condition.age = params.age;
if (params.country) condition.country = params.country;
return condition;
}
Candidate.findOneAndUpdate({
_id:candidateId
},createUpdatedValues(body)).then((data) => {
if (!data) {
res.status(400).send('noDataFound');
} else {
res.status(200).send(data);
}
}).catch((e) => {
res.status(500).send(e)
})
Form a new object that includes sent values:
const newObj = {};
if (body.name) {
newObj.name = body.name;
}
if (body.age) {
newObj.age = body.age;
}
if (body.country) {
newObj.country = body.country;
}
Then update using newObj.
Candidate.findOneAndUpdate({
_id:candidateId
}, newObj)...
I am using the following way to go around it , if any other better approach is there let me know ill mark that as answer.
I am now passing all the data to update from frontend in a new object like this
--- Client Data---
{ "candidateId":"5a9a86c16acff45a8070d2da",
"updatedData":{
"firstName":"Jack",
"lastName":"Bauer",
"applicationSource":"consultant",
"skills":["trading","sales"]
}
}
---- Updating logic ----
var body = _.pick(req.body, ["candidateId","updatedData"]);
var candidateId = mongoose.Types.ObjectId(body.candidateId);
var dataToUpdate = _.pick(body.updatedData,["firstName","lastName"]);
Candidate.findOneAndUpdate({
_id:candidateId
},dataToUpdate).then((data) => {
if (!data) {
res.status(400).send('noDataFound');
} else {
res.status(200).send(data);
}
}).catch((e) => {
res.status(500).send(e)
})

Push to second level array in mongodb with node/express

I am working on a chatroom where users can chat with each other filtered on the basis on projects. Users from the same project can talk to each other.
Here is my chat model where each document is based on project ref and has an array for the messages with user refference:
'use strict';
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var ChatSchema = new mongoose.Schema({
projectid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Project'
},
messages: [{
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
message: String,
date: {
type: Date,
default: Date.now
},
time: String
}]
});
export default mongoose.model('Chat', ChatSchema);
Now I am trying to update the messages array with new messages but I am unable to do so since past few hours. Here is what I have so far.
To get chat messages based on projects I am using:
routes:
router.get('/projectid/:id', controller.showByProject);
router.post('/projectid/:id', controller.insertMessageByProject);
controller:
// Gets the chat thread based on project id
export function showByProject(req, res) {
Chat.findAsync({projectid: req.params.id})
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Insert a new message in the chat based on projectid
export function insertMessageByProject(req, res) {
if (req.body._id) {
delete req.body._id;
}
Chat.findAsync({projectid: req.params.id})
.then(handleEntityNotFound(res))
.then(saveUpdates({$push: {messages: req.body}}))
.then(respondWithResult(res))
.catch(handleError(res));
}
Json Object I am sending from POSTMAN:
{
"messages":
{
"userid": "56d7967745ab81322a964927",
"message": "This is a meesage"
}
}
OR
{
"userid": "56d7967745ab81322a964927",
"message": "This is a meesage"
}
I am able to update the object if I have the object ID to the chat document itself but inside my application, I do not have the direct reference. I have tried few other ways as well but every time my application returns a 500 error.
Your help would be highly appreciated.
EDIT 1: here are the helping functions I am using generated by the angular full-stack plugin.
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}
function saveUpdates(updates) {
return function(entity) {
var updated = _.merge(entity, updates);
return updated.saveAsync()
.spread(updated => {
return updated;
});
};
}
function removeEntity(res) {
return function(entity) {
if (entity) {
return entity.removeAsync()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
EDIT 2: As I mentioned in the comments, the problem was with _.Merge function which was not merging the object right, although it should have been able to update the object.
So I wrote my own function for saveUpdates as follows:
function saveUpdatesForNewChat(updates) {
return function(entity) {
var temp = entity;
temp[0].messages.push(updates);
console.log('\ntemp:');
console.log(require('util').inspect(temp, { depth: null }));
console.log('\nend of ops\n\n');
var updated = _.merge(entity, temp);
console.log('out of merge');
console.log(require('util').inspect(updated, { depth: null }));
return updated.saveAsync()
.spread(updated => {
return updated;
});
};
}
ok so I have left the console logs inside and it's perfect object to save into the database but the server still returns a 500 errors on update.
OK! So I have found the answer myself.
The problem was that the object returned was a result set and I was calling save on whole result set. I fetched the first element out of the returned resultset, pushed new message to the element and called save on it and it started working.
Here is the code:
function saveUpdatesForNewChat(updates) {
return function(entity) {
var temp = entity[0];
temp.messages.push(updates);
var updated = temp;
return updated.saveAsync()
.spread(updated => {
return updated;
});
};
}

Resources