children in a mongoose schema wont save incremented numbers but only once - node.js

I have a user schema and I am able to increment the number and save to DB, but for children of the schema within an array I am also able to increment the number but it wont save to DB.
const userSchema = new Schema({
inDex: {type: Number, default: 0},
userName: {
type: String,
require: true,
unique: true,
},
email: {
type: String,
lowercase: true,
unique: false,
},
password: { type: String, required: true },
mnemonic: {
type: String,
required: true,
},
profiles: [address: {index: {type: Number, default: 0}]
})
const User = mongoose.model('user', userSchema);
async function processUserInput(req, res) {
User.findById({ _id: userId}).then((doc)=> {
doc.inDex = doc.inDex+1 // Will Increment and Save
doc.profiles[0].address.index =
doc.profiles[0].address.index+1 //Will increment BUT WONT SAVE
doc.save()
}).catch(err => console.log('err', err))
}
router.post('/', async (req, res) => {
await processUserInput(req,res)
res.status(200).json(some json data)
})

Related

Cannot Populate path in Mongoose while trying to join two documents

MongooseError: Cannot populate path loaned_to because it is not in your schema. Set the strictPopulate option to false to override.
I've tried to join two documents in mongodb using mongoose in nodejs, But unfortunately this error occurs. My mongoose version is 6.0.6
Book Schema
const mongoose = require('mongoose');
const BookSchema = new mongoose.Schema({
"name": {type: String, required: true},
"author_name": {type: String, required: true},
"published_date": {type: Date, required: false},
"copies": [
{
"isbn_number": {type: String, required: true},
"status": {type: String, required: true, default: "Available"},
"due_back": {type: Date, required: false},
"loaned_to": {type: mongoose.Schema.Types.ObjectId, required: false, ref: "User"}
},
]
})
const Book = mongoose.model("Book", BookSchema);
module.exports = Book;
User Schema
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
"first_name": {type: String, required: true},
"last_name": {type: String, required: true},
"phone_number": {type: String, required: true},
"address": {type: String, required: false},
"user_name":{type: String, required: true},
"password": {type: String, required: true},
"email": {type: String, required: true},
"notifications": [
{
"notification_id" : {type:"string", required:true},
"notification": {type: "string", required: true}
},
]
})
const User = mongoose.model("User", UserSchema);
module.exports = User;
My code to join documents
exports.getAllBooks = async (req, res) => {
try {
let data = await BookModel.findOne().populate("loaned_to");
res.status(200).send({data: [...data], success: true})
} catch (err) {
console.log(err)
res.status(404).send({success: false, msg: err.message})
}
}
exports.getAllBooks = async (req, res) => {
try {
let data = await BookModel.findOne().populate({
path: 'copies.loaned_to',
select:
'first_name lastName phone_number address user_name email notifications',
});
res.status(200).json({ data: [...data], success: true });
} catch (err) {
console.log(err);
res.status(500).json({ success: false, msg: err.message });
}
};
Use nested populate as in the example below(The example assumes that a Token model has a user which in tern has a role and a role has permissions).
This will return a user object associated with the filtered token, with the role the user is assigned to and the permissions assigned to the role.
That is: TokenModel (has relationship to) -> UserModel (has relationship to) -> RoleModel (has relationship to) -> PermissionsModel)
const popObj = {
path: 'user',
options: { sort: { position: -1 } },
populate: {
path: 'role',
select: 'name',
populate: {
path: 'permissions'
}
}
};
const tokenFilter = {is_active: true};
TokenModel.find(userFilter).populate(popObj);

How does module.exports work

I'm using Express and Mongodb to write my first web app and this is probably a noob question, but let's say I were to define a user model in a file called users.js and then called const User = module.exports = mongoose.model('User', UserSchema) somewhere in the file.
Upon importing users.js (via const User = require([path to users.js]) to some other file in the app, why can I then call new User and have access to the model instead of having to call new users.User?
The standard way to define the model and using the schema in the controller
User.js
//User Model
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
id: String,
name: {
type: String,
index: true
},
email: {
type: String,
trim: true,
index: true,
lowercase: true,
unique: true
},
mobile: {
type: String,
trim: true,
index: true,
unique: true,
required: true
},
profilePic: String,
password: { type: String },
locations: [{}],
location: {
type: { type: String, default: 'Point', enum: ['Point'] },
coordinates: { type: [], default: [0, 0] },
name: String,
shortAddress: String
},
address: String,
gender: String,
dob: Date,
signupType: { type: String, enum: ['facebook', 'google'] },
deviceType: String,
createdTime: Date,
updatedTime: Date,
googleToken: String,
facebookToken: String,
fcmToken: String,
facebookLink: String,
facebookId: String,
memberType: String,
deviceId: String,
preferences: [{}],
loginData: [{}],
token:String,
isVerified: Boolean,
isMobileVerified: Boolean,
isEmailVerified: Boolean,
lastSeen: Date
});
// 2D sphere index for user location
userSchema.index({ location: '2dsphere' });
mongoose.model('User', userSchema);
module.exports = mongoose.model('User');
UserController.js
//User Controller
var User = require('./User');
// RETURNS ALL THE USERS IN THE DATABASE
router.get('/', function (req, res) {
User.find({}, function (err, users) {
if (err) return res.status(500).send({ errors: "There was a problem finding the users." });
res.status(200).send(users);
});
});

Mongoose unable to get _id from Schema

I 've a UserSchema that looks like:
export var UserSchema: Schema = new mongoose.Schema({
createdAt: Date,
email: {
type: String,
required: true,
trim: true,
unique: false,
},
firstName: {
type: String,
required: false,
trim: true
},
lastName: {
type: String,
required: false,
trim: true
},
password: {
type: String,
trim: true,
minlength: 6
},
tokens: [{
access: {
type: String,
required: true
},
token: {
type: String,
required: true
}
}]
});
And I 've a instance method like:
UserSchema.methods.printThis = () => {
var user = this;
console.log("========>>> PRINTING USER NOW");
console.log(user);
};
The method printThis is being called from
router.post('/signup', (req, res) => {
var body = _.pick(req.body, ['email', 'password']);
var user = new User(body);
console.log("created user as: ", user);
user.printThis();
});
Below is the output:
created user as: { email: 'prsabodh.r#gmail.com',
password: '123456',
_id: 59be50683606a91647b7a738,
tokens: [] }
========>>> PRINTING USER NOW
{}
You can see that the user is getting created properly. However, when I call printThis method on User - I'm not able to print the same user back and an empty {} is printed. How to fix this?
You shouldn't use arrow functions (=>) if the calling function is explicitly setting a context (which is what Mongoose does):
UserSchema.methods.printThis = function() {
var user = this;
console.log("========>>> PRINTING USER NOW");
console.log(user);
};
More info on arrow functions and their handling of this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#Arrow_functions
To get the _id value from the instance method can use _conditions that should work
UserSchema.methods.printThis = function(password) {
var user = this;
console.log(user._conditions['_id']);
};

Referencing Object Id not working in Mongoose 4.11.6

I have this problem. Basically, I have 2 schemas - a User schema and a Document schema. The Document schema has an owner which references the _id field of documents in the User collection.
The problem is that I am still able to save documents in the Document collection with owner ids that do not exist in the User collection which should not be so.
Here is my User schema and Document schema respectively
const UserSchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
email: {
type: String,
validate: [{ validator: value => isEmail(value), msg: 'Invalid email.'
}],
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
}, {
timestamps: true,
});
const User = mongoose.model('User', UserSchema);
And the Document Schema
const DocumentSchema = new Schema({
title: {
type: String,
required: true,
},
text: {
type: String,
},
access: {
type: String,
enum: ['public', 'private'],
default: 'public',
},
owner: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
}, {
timestamps: true,
});
const Document = mongoose.model('Document', DocumentSchema);
Any help will be appreciated thanks.
For that situation you can add pre save function in your Document schema that will call before save your Document.
const DocumentSchema = new Schema({
// ...
}, {
timestamps: true,
});
DocumentSchema .pre("save",function(next) {
var self = this;
if (self.owner) {
mongoose.models['User'].findOne({_id : self.owner }, function(err, existUser){
if(err){
return next(false, err);
}
if(!existUser)
return next(false, "Invalid user reference");
else
return next(true);
});
} else {
next(false, "Owner is required");
}
});
const Document = mongoose.model('Document', DocumentSchema);

How to access one model Schema in another model mongoose database

I have two schemas one is user schema in user.js file and other is product schema in product.js file
My user schema in user.js file as follows:
var userSchema = new Schema({
firstname: {
type: String,
required: true
},
lastname: {
type: String,
required: true
},
password: {
type: String,
required: true
},
mobileno: {
type: Number,
unique: true,
required: true
},
facebookid: {
type: String
},
userimage: {
type: String
}
});
and I am overriding automatically generated _id using mongoose-auto-increment module to get automatically incremented userId in user collection.
And my product schema in product.js file as follows:
var productSchema = new Schema({
userId: {
type: String,
required: true
},
productName: {
type: String,
required: true
},
productId: {
type: String,
required: true
},
price: {
type: Number,
unique: true,
required: true
},
prodcutImage: {
type: String
}
});
When user will add new products in collection he will fill all the fields mentioned in product schema. I want to verify that entered userId is exists in user collection or not when new product is added by user in product collection.
I tried to access userSchema.find method in productSchema pre save hook
productSchema.pre('save', function (next) {
userSchema.findOne({'_id': userId}, function(err, user)
{
console.log(user);
});
}
But It returns an error. Can somebody help me in this issue.
You can do like this
app.get('/user/:id', function (req, res, next) {
userSchema.findOne({'_id': userId}, function(err, user)
{
if(user){
next()
}else{
res.json("user id is not valid");
}
});
}, function (req, res, next) {
// code to add your product in product schema
})
more better way is to use Router-level middleware of express

Resources