I'm still learning about Node.js & MongoDB so my question is:
on Update functionality would like to update some of the fields not necessary all on them So How i can do that ?
Note: i used the same validateTask function for insert new data to tasks document.
router.put('/:id', (req, res) => {
const { error } = validateTask(req.body);
if(error) return res.status(400).send(error.details[0].message);
Task.findByIdAndUpdate(req.params.id, {
name: req.body.name,
employee: req.body.employee,
description: req.body.description,
section: req.body.section,
status: req.body.status,
updated_at: new Date()
}, { new: true })
.then ( data => {
res.status(201).send(data);
}).catch(err => {
res.status(422).send('The task with given ID was not found');
});
});
function validateTask(task) {
const schema = {
name: Joi.string().min(3).required(),
employee: Joi.string().min(3).required(),
description: Joi.string().min(3).required(),
section: Joi.string().min(3).required(),
status: Joi.boolean()
};
return Joi.validate(task, schema);
}
Remove the validateTask validation
The error is because in validateTask function you have made all the fields as required and these paramters are validated before findByIdAndUpdate.
I believe You need not have any required(mandatory field) validation to be done in update. This validation can be only done for documents that are newly inserted
For that, you can use Task.findByIdAndUpdate it will only update the fields you give to for example
User: {
name: Ahmed Gamal
email: ahmed#ahmedgamal.ga
country: Egypt
}
When used with findByIdAndUpdate and given {name: Ahmed} the result will be
User: {
name: Ahmed
email: ahmed#ahmedgamal.ga
country: Egypt
}
Related
I'm setting up simple API using Postgresql, Knex.js and Objection.js. I created User model with "location" property. This "location" property is another table. How I have to insert that user to database with defaults 'city' and 'country' in 'location' property?
I already tried to use 'static get jsonSchema' in model itself and 'allowInsert' method in mutation but when I fetching created that user the 'location' still 'null'.
So, let's say we have migration for users_table:
exports.up = knex =>
knex.schema.createTable('users', table => {
table.increments('id').primary();
table
.string('email')
.unique()
.notNullable();
table.string('firstName').notNullable();
table.string('lastName').notNullable();
table.string('password').notNullable();
});
exports.down = knex => knex.schema.dropTable('users');
And we have location_table:
exports.up = knex =>
knex.schema.createTable('locations', table => {
table.increments('id').primary();
table.string('country').defaultTo('USA');
table.string('city').defaultTo('San Francisco');
table
.integer('user_id')
.references('id')
.inTable('users')
.onUpdate('CASCADE')
.onDelete('CASCADE');
});
exports.down = knex => knex.schema.dropTable('locations');
Here User Model with objection.js:
export default class User extends Model {
static get tableName() {
return 'users';
}
// wrong probably
static get jsonSchema() {
return {
type: 'object',
properties: {
location: {
type: 'object',
properties: {
city: {
type: 'string',
default: 'Los Angeles',
},
country: {
type: 'string',
default: 'USA',
},
},
},
},
};
}
fullName() {
return `${this.firstName} ${this.lastName}`;
}
static get relationMappings() {
return {
location: {
relation: Model.HasOneRelation,
modelClass: Location,
join: {
from: 'users.id',
to: 'locations.user_id',
},
},
};
}
}
And Location model:
export default class Location extends Model {
static get tableName() {
return 'locations';
}
static get relationMappings() {
return {
user: {
relation: Model.BelongsToOneRelation,
modelClass: `${__dirname}/User`,
join: {
from: 'locations.user_id',
to: 'users.id',
},
},
};
}
}
My mutation when I creating new User:
// ...
const payload = {
email,
firstName,
lastName,
password: hash,
};
const newUser = await User.query()
.allowInsert('[user, location]')
.insertAndFetch(payload);
// ...
And in the end query:
// ...
User.query()
.eager('location')
.findOne({ email });
// ...
From query of user I expect to see the object with locatoin propety with my defaults. Example:
{
email: 'jacklondon#gmail.com',
firstName: 'Jack',
fullName: 'Jack London',
id: '1',
lastName: 'London',
location: {
city: 'San Francisco',
country: 'USA',
},
userName: 'jacklondon1',
__typename: 'User',
}
So, where I made mistake with such simple operation?
One to One Solution
I think part of the issue is that your allow insert included the user object. You shouldn't include the user in the allow insert because it's implicit since you're on the User model (example). The other issue you had was that you were trying to use insertAndFetch method. insertAndFetch cannot be used when inserting a graph. You need to use the insertGraph method to insert a graph (docs). Since you are using Postgres, you can chain the returning(*) method and it will return the result without additional queries (example). Finally, since you're asking for a one-to-one relation, you have to specify a city and country every time. Objection will not know it needs to create a new row without specifying it (even if you have configured the database to have default values). The way I accomplished this for you was to use default parameters.
const createNewuser = async (email, firstName, lastName, hash, city = 'Los Angeles', country = 'USA') => {
const newUser = await User
.query()
.insertGraph({
email,
firstName,
lastName,
password: hash,
location: {
city: city,
country: country
}
})
.returning('*');
return newUser;
}
Additional Thought to Ponder
I'm not sure why you have a one-to-one relationship between user and location. Why not just make city, state, and country part of the user's table since it's already one to one?
However, what I think you're really going for is a one-to-many relationship between user and location. One location has multiple users. This would put you into 3rd normal form by reducing the amount of duplicate data in your database since you wouldn't duplicate a city/country for each user in an identical location.
If you're just learning objection, I would recommend reading up on graphs in the documentation.
I have a post route that receives data from a PUT request in an express app that aims to update a mongoose document based on submitted form input. The "Base" model is Profile, and I have two discriminator models Helper and Finder that conditionally add fields to the Profile schema (see below for details).
Thus, req.body.profile will contain different fields depending on the discriminator it's associated with, but will always contain the fields (username, email city, accountType) present in the "base" model, Profile.
Before I send my PUT request, an example of a document in Profile looks like this:
{ jobTitle: '',
lastPosition: '',
email: '',
city: '',
accountType: 'helper',
_id: 5c77883d8db04c921db5f635,
username: 'here2help',
__v: 0 }
This looks good to me, and suggests that the model is being created as I want (with base fields from Profile, and those associated with the Helper model - see below for models).
My POST route then looks like this:
router.put("/profile/:id", middleware.checkProfileOwnership, function(req, res){
console.log(req.body.profile);
Profile.findOneAndUpdate(req.params.id, req.body.profile, function(err, updatedProfile){
if(err){
console.log(err.message);
res.redirect("/profile");
} else {
console.log(updatedProfile);
res.redirect("/profile/" + req.params.id);
}
});
});
The information I receive from the form (console.log(req.body.profile)) is what I expect to see:
{ accountType: 'helper',
username: 'here2help',
email: 'helpingU#me.com',
city: 'New York',
jobTitle: 'CEO',
lastPosition: 'sales rep'}
However, after updating the document with req.body.profile in Profile.findOneAndUpdate(), I do not see my returned document updated:
console.log(updatedProfile)
{ jobTitle: '',
lastPosition: '',
email: 'helpingu#me.com',
city: 'New York',
accountType: 'helper',
_id: 5c77883d8db04c921db5f635,
username: 'here2help',
__v: 0 }
So, the fields that are defined in my 'Base' model (ie those defined in ProfileSchema - see below) are being updated (e.g. city), but those that are in my discriminators are not - see below.
The updated information is clearly present in req, but is not propagated to the Profile model - How can this be?
I've also tried using findByIdAndUpdate but I get the same result.
Here are the Schemas I'm defining:
Profile - my "base" schema:
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var profileSchema = new mongoose.Schema({
username: String,
complete: { type: Boolean, default: false },
email: { type: String, default: "" },
city: { type: String, default: "" }
}, { discriminatorKey: 'accountType' });
profileSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("Profile", profileSchema);
Finder
var Profile = require('./profile');
var Finder = Profile.discriminator('finder', new mongoose.Schema({
position: { type: String, default: "" },
skills: Array
}));
module.exports = mongoose.model("Finder");
Helper
var Profile = require('./profile');
var Helper = Profile.discriminator('helper', new mongoose.Schema({
jobTitle: { type: String, default: "" },
lastPosition: { type: String, default: "" }
}));
module.exports = mongoose.model("Helper");
This is my first attempt at using discriminators in mongoose, so it's more than possible that I am setting them up incorrectly, and that this is the root of the problem.
Please let me know if this is unclear, or I need to add more information.
It matters what schema you use to query database
Discriminators build the mongo queries based on the object you use. For instance, If you enable debugging on mongo using mongoose.set('debug', true) and run Profile.findOneAndUpdate() you should see something like:
Mongoose: profiles.findAndModify({
_id: ObjectId("5c78519e61f4b69da677a87a")
}, [], {
'$set': {
email: 'finder#me.com',
city: 'New York',
accountType: 'helper',
username: 'User NAme', __v: 0 } }, { new: true, upsert: false, remove: false, projection: {} })
Notice it uses only the fields defined in Profile schema.
If you use Helper, you would get something like:
profiles.findAndModify({
accountType: 'helper',
_id: ObjectId("5c78519e61f4b69da677a87a")
}, [], {
'$set': {
jobTitle: 'CTO',
email: 'finder#me.com',
city: 'New York',
accountType: 'helper ',
username: 'User Name', __v: 0 } }, { new: true, upsert: false, remove: false, projection: {} })
Notice it adds the discriminator field in the filter criteria, this is documented:
Discriminator models are special; they attach the discriminator key to queries. In other words, find(), count(), aggregate(), etc. are smart enough to account for discriminators.
So what you need to do when updating is to use the discriminator field in order to know which Schema to use when calling update statement:
app.put("/profile/:id", function(req, res){
console.log(req.body);
if(ObjectId.isValid(req.params.id)) {
switch(req.body.accountType) {
case 'helper':
schema = Helper;
break;
case 'finder':
schema = Finder;
break;
default:
schema = Profile;
}
schema.findOneAndUpdate({ _id: req.params.id }, { $set : req.body }, { new: true, upsert: false, remove: {}, fields: {} }, function(err, updatedProfile){
if(err){
console.log(err);
res.json(err);
} else {
console.log(updatedProfile);
res.json(updatedProfile);
}
});
} else {
res.json({ error: "Invalid ObjectId"});
} });
Notice, above is not necessary when creating a new document, in that scenario mongoose is able to determine which discriminator to use.
You cannot update discriminator field
Above behavior has a side effect, you cannot update the discriminator field because it will not find the record. In this scenario, you would need to access the collection directly and update the document, as well as define what would happen with the fields that belong to the other discriminator.
db.profile.findOneAndUpdate({ _id: req.params.id }, { $set : req.body }, { new: true, upsert: false, remove: {}, fields: {} }, function(err, updatedProfile){
if(err) {
res.json(err);
} else {
console.log(updatedProfile);
res.json(updatedProfile);
}
});
Please add option in findOneAndUpdate - { new: true };
In Moongose findOneAndUpdate() Method have four parameters
like
A.findOneAndUpdate(conditions, update, options, callback) // executes
And you need to execute like this
var query = { name: 'borne' };
Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)
or even
// is sent as
Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options, callback)
This helps prevent accidentally overwriting your document with { name: 'jason bourne' }.
Pretty new to Node.js and Mongoose.
Trying to perform a basic set action after finding the relevant object, however I get the following error:
TypeError: {found object}.set is not a function.
The following is the code causing the error:
UserProfile.find({"user": req.params.id}, function (err, userProfile) {
if (err) {
console.log("Saving User profile - Error finding user");
} else { // no error
if (userProfile) { // if userProfile is found
console.log("Saving User profile - userProfile found for user: " + userProfile);
userProfile.set ({
gender: req.body.gender,
dob: req.body.dob,
phone: req.body.phone,
phone2: req.body.phone2,
state: req.body.state,
country: req.body.country
});
}
}
});
The following is the error i receive:
TypeError: userProfile.set is not a function
If I'm trying to use the "set" function on a new object created based on the same model, it works with no issue
var userProfile = new UserProfile ();
userProfile.set ({
gender: req.body.gender,
dob: req.body.dob,
phone: req.body.phone,
phone2: req.body.phone2,
state: req.body.state,
country: req.body.country
});
The following is the model:
var mongoose = require("mongoose");
var UserProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
gender: String,
phone: String,
phone2: String,
dob: Date
});
module.exports = mongoose.model ("UserProfile", UserProfileSchema);
Use findOne not find. The former returns an object as the 2nd argument in the callback, the latter returns an array as the 2nd argument in the callback.
.find returns an array of documents. try using .findOne which returns the first found document
I have an endpoint that joins the user and user_emails table as a one-to-many relationship (postgresql). It look as follows.
router.get('/', function (req, res, next) {
db.select('users.id', 'users.name', 'user_emails.address')
.from('users')
.leftJoin('user_emails', 'users.id', 'user_emails.user_id')
.then(users => res.status(200).json(users))
.catch(next) // go to error handler
});
However, this will return a new document for each email address. What I want is an array of documents that looks as follows:
[{
id: 1,
name: 'Steve',
emails: [
{ address: 'hello#world.org' },
{ address: 'meow#meow.org' }
]
}, {
id: 2,
name: 'Jimmy',
emails: [
{ address: 'jimmy#jimbo.org' }
]
}]
How should this be done in knex?
Assuming you're using Postgres - you need to use array_agg function to generate arrays. I would suggest using knex.raw
Please let me know if this works.
knex('users')
.innerJoin('user_emails','users.id','user_emails.user_id')
.select([
'users.id as userID',
'users.name as userName',
knex.raw('ARRAY_AGG(user_emails.adress) as email')
])
.groupBy('users.id','users.name')
I got 3 database models in mongoose that looks like this:
//profile.js
var ProfileSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
matches: [{ type: Schema.Types.ObjectId, ref: 'Match' }]
});
//match.js
var MatchSchema = new Schema({
scores: [{ type: Schema.Types.ObjectId, ref: 'Score', required: true }],
});
//score.js
var ScoreSchema = new Schema({
score: {type: Number, required: true},
achivement: [{type: String, required: true}],
});
And I try to populate a profile with
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches')
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});
The matches get populated but I dont get the scores in matches to populate. Is this not supported in mongoose or do I do something wrong? Populate gives me this:
{
user_token: "539b07397c045fc00efc8b84"
username: "username002"
sex: 0
country: "SE"
friends: []
-matches: [
-{
__v: 1
_id: "539eddf9eac17bb8185b950c"
-scores: [
"539ee1c876f274701e17c068"
"539ee1c876f274701e17c069"
"539ee1c876f274701e17c06a"
]
}
]
}
But I want to populate the score array in the match array. Can I do this?
Yes, you are right. I tried using Chaining of populate I got same output.
For your query please use async.js and then populate by the method mentioned below.
For more details please have a look at this code snippet. It is a working, tested, sample code according to your query. Please go through the commented code for better understanding in the code below and the link of the snippet provided.
//Find the profile and populate all matches related to that profile
Profile.findOne({
_id: mongoose.Types.ObjectId(profile_id)
})
.populate('matches')
.exec(function(err, profile) {
if (err) throw err;
//We got the profile and populated matches in Array Form
if (profile) {
// Now there are multiple Matches
// We want to fetch score of each Match
// for that particular profile
// For each match related to that profile
async.forEach(profile.matches, function(match) {
console.log(match, 'match')
// For each match related to that profile
// Populate score achieved by that person
Match.find({
_id:match.id
})
.populate('scores', 'score')
.exec(function (err, score) {
if (err) throw err;
// here is score of all the matches
// played by the person whose profile id
// is passed
console.log(score);
})
})
}
});
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});