I am new to Mongoose and couldn't find an answer elsewhere.
I have a user schema like this:
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
admin: {
type: Boolean,
default: true
},
writer: {
type: Boolean,
default: true
},
producer: {
type: Boolean,
default: true
}
})
And I want to get someone name by their _id
I have this: Users.findById(posts.userID).schema.obj.name but it obviously doesn't return a name, thanks!
findById returns a single document containing the actual values for the properties you've defined in your schema. So if you're just interested in getting the name from the resulting document you can do:
const user = await Users.findById(posts.userID);
const name = user.name;
Any request to mongo via mongoose is asynchronus.
So .findById method return promise-like object.
You need to wait for result via one of three ways:
Pass callback function like
Users.findById(id, function (err, user) {
console.log(user.name);
});
Use .then() like with promise
Users.findById(id).then((user) => {
console.log(user.name);
});
Use async/await:
async function getUser(id) {
const user = await Users.findById(id);
console.log(user.name);
};
Related
I'm trying to access a MongoDB database using Node.js and Mongoose.
I created a virtual property in Schema called username. See the code that follows.
const mongoose = require("mongoose");
const User = require("../models/user");
const datatypes = ['temperature', 'humidity'];
const nodeSchema = new mongoose.Schema(
{
MACAddress: {
type: String,
required: true,
trim: true,
uppercase: true,
match: /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/,
},
alias: {
type: String,
trim: true,
},
coordinates: {
type: String,
required: false,
match: /^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/,
},
address: {
type: String,
required: false,
},
userID: {
type: mongoose.Types.ObjectId,
},
nodeType: {
type: String,
enum: ['router', 'node'],
default: 'node',
},
dataTypes: {
type: [String],
enum: datatypes,
required: true,
}
},
{
timestamps: true,
}
);
The virtual property is used to set the userID property. See the code that follows.
// virtual field
nodeSchema.virtual("username").set(async function (username) {
this.userID = await this.getUserID(username);
});
// methods
nodeSchema.methods = {
getUserID: function (username) {
if (!username) return null;
return User.find({username: username}).then(userDoc => userDoc[0]._id);
},
};
To add a new document to the database, I am using the following code.
const newNode = new Node(newNodeData);
newNode.save().then( (node) => {
console.log(node.userID)
}
)
The problem is this... Calling the User.find function returns a promise. Even using await (see previous code), newNode.save() saves the document in the database without the userID property.
If I change the code to the following snippet, which doesn't use promise, the userID property is saved in the database with no problem. However, this is not what I want.
// virtual field
nodeSchema.virtual("username").set(async function (username) {
let ObjectId = mongoose.Types.ObjectId;
this.userID = new ObjectId("6245e896afe465a25047302e");
});
How can I force newNode.save() to wait for the promise result before saving the document to the database?
Is there a way for me to be able to use req.user outside of a post or get request? I am asking this because I want to use this variable somewhere else. The req.user is from the user schema which is defined like this:
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
health: {
type: Number,
},
energy: {
type: Number,
},
attack: {
type: Number,
},
defence: {
type: Number,
},
endurance: {
type: Number
},
exp: {
type: Number
},
power: {
type: Number
},
charactername: {
type: String
},
characterimg: {
type: String
},
alive: {
type: String
},
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
Now is there a way for me to acces req.user outside a get or post request? Or can I access that schema in another way which does not include the `req.user?
To give you a small example:
router.post('/mission1btn', ensureAuthenticated, function(req, res){
const user = req.user
Above is where I use it, but now I would like to use it outside of that. In for example this place:
function check() {
if (user.endurance = 100) {
//do something
}
}
consider your Mongoose user definition is in file models/User.js.
in any other code you can find one of users from database and perform actions with it using this code
"use strict";
const User = require('./models/User.js');
User.findOne({name:"test"}, function(error,user) {
if(error) {
throw error;
}
// user here is the same object as `req.user`
console.log(user.name); // outputs "test"
if (user.endurance = 100) {
//do something
}
});
So I have collections of Teacher, Classes. to understand my problem its necessary to understand my database. the structure is
const TeacherSchema = new Schema(
{
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
role: {
type: String,
default: "teacher"
},
userid: {
type: String,
required: true
},
password: {
type: String,
required: true
},
profileImage: {
type: String,
required: false
},
classes: [{
type:Schema.Types.ObjectId,
ref:'class'
}]//1 teacher will have multiple classes
},
{ timestamps: true }
);
and the class:
const ClassSchema = new Schema(
{
subject: {
type: String,
required: true
},
teacher:[{
type:Schema.Types.ObjectId,
ref: 'teacher'
}],//once class will have only one teacher
},
{ timestamps: true }
);
What i want to do is, I want to show teacher how many classes he/she has. here is my controller.
exports.getAllClass= async (req, res, next) => {
let teacherClasses
try{
teacherClasses= await Teacher.findById(req.params.id)//teacher database
arrayOfClass= teacherClasses.classes //getting the class it has array of objectID
arrayOfClass.forEach(async (classes)=>{
let classDB =await Class.findById(classes)
console.log(classDB.subject)// in the console it shows all the classes.
return res.status(200).json({
'name':classDB.subject //but here it only shows one class,the first class name
})
});
}catch(err){
console.log(err)
}
}
I have tried many thing, but cant reach to a proper solution. Can you tell me what did i do wrong here or is there any better approach? I want to show all the classes in the return. I am new in programming hence assigned in complex task, please help me.
You are using forEach on the arrayOfClass and within that forEach you return and send the response. So as soon as the first promise is resolved, the response is sent.
One simple way to do this is to use a for .. of loop and await each class-promise inside and keep track of each result. Once that's done you can send the response:
...
const classes = [];
for(const classId of arrayofClass) {
const classDB = await Class.findById(classId);
classes.push({ name: classDB.subject });
}
return res.json(classes);
Another way to do this is to use Promise.all() which might be better if you're dealing with a bigger dataset as is processes in parallel whereas the first solution processes in sequence:
...
const classPromises = await Promise.all(arrayofClass.map(classId => Class.findById(classId)));
const result = classPromises.map(classDB => ({
name: classDB.subject
}));
return res.json(result);
In a API(Route) call, I wish to have 3 mongoose query and then combine results to form a response json.
Query
student
.countDocuments
(
{}
)
.then(stundentNumber => {
return stundentNumber
})
teacher
.countDocuments
(
{}
)
.then(teacherNumber => {
return teacherNumber;
})
staff
.countDocuments
(
{}
)
.then(staffNumber => {
return staffNumber;
});
Desired Response
res.json({
teacher: teacherNumber,
student: stundentNumber,
staff: staffNumber
});
How can it be done using nodejs/mongoose
If you're using async-await's then try as like below where Promise.all() will help you to execute all operations in parallel :
async function getCounts() {
let [student,teacher,staff] = await Promise.all([student.countDocuments({}),teacher.countDocuments({}),staff.countDocuments({})]);
return {student,teacher,staff};
}
/** call this function in main handler function where you get API call */
getCounts().then((data)=>{res.json(data)}).catch((err)=>{console.log(err)})
You must build student,teacher and staff model
const mongoose = require("mongoose"),
{Schema} = mongoose,
studentSchema = new Schema(
{
name: {
first: {
type: String,
trim: true
},
last: {
type: String,
trim: true
}
},
studentNumber: {
type: Number,
required: true,
lowercase: true,
unique: true
},
}
Same model for the other two.
After that,you must write function query. Read more about queries.
I have multiple questions, please go through my code.
1) how to pass constants/predefined mandatory values through model?
For eg. I have some fields which user must be passing the values and some constants to pass on inside the kafkaSchema.config[ ] and also livySchema.args[ ]. The code i want to pass through is in second question on the same question thread.
const mongoose = require('mongoose');
const livy_schema = mongoose.Schema({
file: { type: String, required: true },
name: { type: String, required: true },
className: { type: String, required: true },
args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to
});
const kafka_schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true, unique: false },
config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to
});
const enrichedEventSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
name: { type: String, required: true, unique: true },
description: { type: String, required: false },
type: { type: String, enum: ["Enriched"], required: true },
format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
kafka: [kafka_schema],
livy: [livy_schema] // how to make this schema required:true?
});
module.exports = mongoose.model('EnrichedEvent', enrichedEventSchema);
2) how to make this code to run asynchronously, Right now its working synchronously. For example, Its able to save the eventdata in event collection in database, then its updating the project collection, then calling axios.post method to call my livy server and kafka server in order. What i want to do is save the eventdata in event collection in database, then update the project collection (synchronously), meanwhile I want to call my livy and kafka server at the same time (Asynchronously).
router.post("/:projectId/events/enriched", (req, res, next) => {
const enrichedEvent = new EnrichedEvent({
_id: mongoose.Types.ObjectId(),
name: req.body.name,
projectId: req.params.projectId, //taking from url
description: req.body.description,
type: req.body.type,
format: req.body.format,
kafka: req.body.kafka,
livy: req.body.livy
});
enrichedEvent.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Event stored",
createdEvent: {
_id: result._id,
projectId: result.projectId,
name: result.name,
description: result.description,
type: result.type,
kafka: result.kafka,
livy: result.livy
}
});
Project.findOneAndUpdate({ _id: result.projectId },
{ $push: { enrichedEvents: result._id } })
axios.post("http://52.xxx.xxx.199:8998/batches", result.livy)
.then(function (response) {
console.log(response);
})
.then(axios.get("http://52.xxx.xxx.199:8998/batches/"), function (res) {
console.log(res);
})
axios.post("http://52.xxx.xxx.199:8083/connectors", result.kafka)
.then(function (response) {
console.log(response);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
});
Question may seem bit lengthy, but valid question to ask on SO. Please guide me to right direction.
1)
const enrichedEventSchema = mongoose.Schema({
// ...
livy: { type: [livy_schema], required: true }
});
2)
return enrichedEvent.save().
then(result => {
// ...
return Project.findOneAndUpdate(/*...*/);
}).
then(() => {
// ...
return Promise.all([axios.post(/*...*/), axios.post(/*...*/]);
});
Hey try the following:
1) for saving user's entered configurations and also having default constants. You could use mongoose pre save hook.
https://mongoosejs.com/docs/middleware.html#pre
livy_schema.pre('save', function(next) {
this.args = { ...this.args, ...CONSTANTS }; //I'm use es6's spread operator
next();
});
kafka_schema.pre('save', function(next) {
this.config = { ...this.config, ...CONSTANTS }; //I'm use es6's spread operator
next();
});
2) For second question: try following:
axios.all([
axios.post("http://52.221.178.199:8998/batches", result.livy),
axios.post("http://52.221.178.199:8083/connectors", result.kafka)
]);