I want to populate the busNumber from bus Bus table to the trip table.
Here's the bus model
const busSchema = new mongoose.Schema(
{
busNumber: {
type: String,
unique: true,
required: true,
},
seats: {
type: Number,
},
},
{
timestamps: true,
}
);
now I want to show the bus number inside the trip table instead of bus._id. I know how to exclude data but don't know how to include data from other collections.
here's the route model where I included the bus model
const routeSchema = new mongoose.Schema({
location:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Location',
required: true
},
duration: {
type: Number,
required: true,
},
Bus:{
type: mongoose.Schema.Types.ObjectId,
ref:"Bus",
required: true
},
date: {
type:String,
required: true
},
},
{
timestamps: true,
});
here's the query:
router.get("/trips", async (req, res) => {
if ((!req.query.departure && !req.query.arrival) || !req.query.date) {
return res.send({
error: "Please enter the data to get the trip",
});
}
const { departure, arrival, date } = req.query;
const locations = await Location.find({
"departureLocation.name": departure,
"arrivalLocation.name": arrival,
});
const ids = locations.map(location => location._id);
const routes = await Route.find({
$and: [{ location: { $in: ids } }, { date }],
}).select(['-busId', '-location', '-_id', '-createdAt', '-updatedAt', '-__v']);
return !routes ? res.status(404).send() : res.status(200).send(routes);
});
Here's the result I am getting https://i.stack.imgur.com/AwK5N.png
How to use the populate() function to get data from another collection in mongoose
use this code for your populate Bus key
router.get("/trips", async (req, res) => {
if ((!req.query.departure && !req.query.arrival) || !req.query.date) {
return res.send({
error: "Please enter the data to get the trip",
});
}
const { departure, arrival, date } = req.query;
const locations = await Location.find({
"departureLocation.name": departure,
"arrivalLocation.name": arrival,
});
const ids = locations.map(location => location._id);
const routes = await Route.find({
$and: [{ location: { $in: ids } }, { date }],
}).populate("Bus").select(['-busId', '-location', '-_id', '-createdAt', '-updatedAt', '-__v']);
return !routes ? res.status(404).send() : res.status(200).send(routes);
});
Related
I have this opportunity model that has this field likes, which is an array of users. How do I check first if the user exist already in that array and if they do I pull them and if they don't I push them back I am building a like creteria for posts
Here is my opportunity model
const mongoose = require("mongoose");
const OpportunityModel = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
content: {
type: String,
required: true,
trim: true,
},
likes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
image: [
{
type: Object,
},
],
share_with_thoughts: {
type: mongoose.Schema.Types.ObjectId,
ref: "Shares",
},
comments: {
type: mongoose.Schema.Types.ObjectId,
ref: "Comment",
},
is_opportunity_applied: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
});
const Opportunity = mongoose.model("Opportunity", OpportunityModel);
module.exports = Opportunity;
What I tried doing but did not work
const likeOrUnlikeOpportunity = expressAsyncHandler(async (req, res) => {
let userId = req.user._id;
let opportunityId = req.params.opportunityId;
let isUserExist = await Opportunity.find({
$and: [{ _id: opportunityId }, { likes: { $elemMatch: { $eq: userId } } }],
}).populate("user", "user_id user_name");
if (isUserExist.length > 0) {
const unliked = await Opportunity.findByIdAndUpdate(
opportunityId,
{
$pull: { users: userId },
},
{ new: true }
).populate("user", "user_id user_name");
if (!unliked) {
res.status(500).send({ ErrMessaage: "an error occured" });
} else {
res.status(200).json(unliked);
}
} else {
const added = await Opportunity.findByIdAndUpdate(
opportunityId,
{
$push: { users: userId },
},
{ new: true }
).populate("user", "user_id user_name");
if (!added) {
res.status(500).send({ ErrMessaage: "an error occured" });
} else {
res.status(200).json(added);
}
}
});
In this case, the action should be dynamic [like | unlike] function, You don't have to chain the function since the like and unlike function/api can't be called at the same time.
Make Your Query Conditional;
Just make sure you have a way to identify between like and unlike.
const {like, postId} = req.body;
let query = {[`${'$' + (like ? 'push' : 'pull')}`]: {likes: userId}};
//assumes that you have the post id
// you can decide not to wait for it to update, just to be sure it did update
await post.findByIdAndUpdate(postId, query).exec()
I hope this helps.
I am building a bus ticket booking app in node.js. I have created 4 tables. 1 - users table, 2 - bus table, 3 - booking table, 4 - route table.
here's the trip model:
const routeSchema = new mongoose.Schema({
departureLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time: {
type: String,
required: true
}
},
arrivalLocation: {
name: {
type: String,
required: true,
},
subLocation: { type: [String] },
time : {
type: String,
required: true
}
},
duration: {
type: Number,
required: true,
},
busId:{
type: mongoose.Schema.Types.ObjectId,
ref:"Bus",
required: true
},
date: {
type:String,
required: true
},
},
{
timestamps: true,
});
In that trip model only administrator(authenticated user) can add data about trip(like departure-Location, arrival-Location, bus-data and date)
router.post("/addTrip", auth, async (req, res) => {
const route = new Route(req.body);
try {
await route.save();
res.status(201).send(route);
} catch (e) {
res.status(500).send();
}
});
suppose there are search boxes for user to enter the details of the trip like this one
https://i.stack.imgur.com/oXvsj.png
User enters the data and that data converted into query string (like this: 127.0.0.1:3000/trips?departure=surat&arrival=bhavnagar&date=2022-05-30) and based on that query string I want to show the all matched trips to the user.
now I want to filter the data according to user's(non-authenticated users as well) need but I don't know how to do that.
router.get("/trips", async (req, res) => {
if(!req.query.departure || !req.query.arrival || !req.query.date){
return res.send({
error: "Please enter the data to get the trip"})
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find().lean().exec();
let route = routes.find((route) => {
route.departureLocation.name.toLowerCase() == departure &&
route.arrivalLocation.name.toLowerCase() == arrival &&
route.date == date;
//What to write here
});
})
I have embedded the seat data in the bus model
const busSchema = new mongoose.Schema(
{
busNumber: {
type: String,
unique: true,
required: true,
},
seats: {
type: Number,
required: true
},
},
{
timestamps: true,
}
);
how to show the users the bus and seats available for that matched trips
You can filter the data using the find function:
router.get('/trips', async (req, res) => {
if (!req.query.departure || !req.query.arrival || !req.query.date) {
return res.send({
error: 'Please enter the data to get the trip',
});
}
let departure = req.query.departure;
let arrival = req.query.arrival;
let date = req.query.date;
let routes = await Route.find({
departureLocation: departure,
arrivalLocation: arrival,
date
}).lean().exec();
return res.status(200).json(routes);
});
Im trying to make an ecommerce app and the frontend is all done but when i place an order i get this message.
I rechecked every file but i cant find where its coming from. I followed the process from a udemy course but it just doesnt work. i got no response from the instructor and his code seems to work fine.
Here is the github master repo for the course.
I am stuck here for 10 days. HELP!
https://github.com/bluebits-academy/mern-stack-ecommerce
This is my Order.js
const mongoose = require('mongoose');
const orderSchema = mongoose.Schema({
orderItems: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'OrderItem',
required:true
}],
shippingAddress1: {
type: String,
required: true,
},
shippingAddress2: {
type: String,
},
city: {
type: String,
required: true,
},
zip: {
type: String,
required: true,
},
country: {
type: String,
required: true,
},
phone: {
type: String,
required: true,
},
status: {
type: String,
required: true,
default: 'Pending',
},
totalPrice: {
type: Number,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
dateOrdered: {
type: Date,
default: Date.now,
},
})
orderSchema.virtual('id').get(function () {
return this._id.toHexString();
});
orderSchema.set('toJSON', {
virtuals: true,
});
exports.Order = mongoose.model('Order', orderSchema);
/**
Order Example:
{
"orderItems" : [
{
"quantity": 3,
"product" : "5fcfc406ae79b0a6a90d2585"
},
{
"quantity": 2,
"product" : "5fd293c7d3abe7295b1403c4"
}
],
"shippingAddress1" : "Flowers Street , 45",
"shippingAddress2" : "1-B",
"city": "Prague",
"zip": "00000",
"country": "Czech Republic",
"phone": "+420702241333",
"user": "5fd51bc7e39ba856244a3b44"
}
**/
This is my api for order. orders.js
const {Order} = require('../models/order');
const express = require('express');
const { OrderItem } = require('../models/order-item');
const router = express.Router();
router.get(`/`, async (req, res) =>{
const orderList = await Order.find().populate('user', 'name').sort({'dateOrdered': -1});
if(!orderList) {
res.status(500).json({success: false})
}
res.send(orderList);
})
router.get(`/:id`, async (req, res) =>{
const order = await Order.findById(req.params.id)
.populate('user', 'name')
.populate({
path: 'orderItems', populate: {
path : 'product', populate: 'category'}
});
if(!order) {
res.status(500).json({success: false})
}
res.send(order);
})
router.post('/', async (req,res)=>{
const orderItemsIds = Promise.all(req.body.orderItems.map(async (orderItem) =>{
let newOrderItem = new OrderItem({
quantity: orderItem.quantity,
product: orderItem.product
})
newOrderItem = await newOrderItem.save();
return newOrderItem._id;
}))
const orderItemsIdsResolved = await orderItemsIds;
const totalPrices = await Promise.all(orderItemsIdsResolved.map(async (orderItemId)=>{
const orderItem = await OrderItem.findById(orderItemId).populate('product', 'price');
const totalPrice = orderItem.product.price * orderItem.quantity;
return totalPrice
}))
const totalPrice = totalPrices.reduce((a,b) => a +b , 0);
let order = new Order({
orderItems: orderItemsIdsResolved,
shippingAddress1: req.body.shippingAddress1,
shippingAddress2: req.body.shippingAddress2,
city: req.body.city,
zip: req.body.zip,
country: req.body.country,
phone: req.body.phone,
status: req.body.status,
totalPrice: totalPrice,
user: req.body.user,
})
order = await order.save();
if(!order)
return res.status(400).send('the order cannot be created!')
res.send(order);
})
router.put('/:id',async (req, res)=> {
const order = await Order.findByIdAndUpdate(
req.params.id,
{
status: req.body.status
},
{ new: true}
)
if(!order)
return res.status(400).send('the order cannot be update!')
res.send(order);
})
router.delete('/:id', (req, res)=>{
Order.findByIdAndRemove(req.params.id).then(async order =>{
if(order) {
await order.orderItems.map(async orderItem => {
await OrderItem.findByIdAndRemove(orderItem)
})
return res.status(200).json({success: true, message: 'the order is deleted!'})
} else {
return res.status(404).json({success: false , message: "order not found!"})
}
}).catch(err=>{
return res.status(500).json({success: false, error: err})
})
})
router.get('/get/totalsales', async (req, res)=> {
const totalSales= await Order.aggregate([
{ $group: { _id: null , totalsales : { $sum : '$totalPrice'}}}
])
if(!totalSales) {
return res.status(400).send('The order sales cannot be generated')
}
res.send({totalsales: totalSales.pop().totalsales})
})
router.get(`/get/count`, async (req, res) =>{
const orderCount = await Order.countDocuments((count) => count)
if(!orderCount) {
res.status(500).json({success: false})
}
res.send({
orderCount: orderCount
});
})
router.get(`/get/userorders/:userid`, async (req, res) =>{
const userOrderList = await Order.find({user: req.params.userid}).populate({
path: 'orderItems', populate: {
path : 'product', populate: 'category'}
}).sort({'dateOrdered': -1});
if(!userOrderList) {
res.status(500).json({success: false})
}
res.send(userOrderList);
})
module.exports =router;
And this is my order-item.js
const mongoose = require('mongoose');
const orderItemSchema = mongoose.Schema({
quantity: {
type: Number,
required: true
},
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
})
exports.OrderItem = mongoose.model('OrderItem', orderItemSchema);
Do comment if you need any more codes.
The error :
ValidationError : OrderItem validation failed : product : Cast to ObjectId failed for value "{..}" at path "product"
means that: the "product" property in OrderItem expect an ObjectId, you can see it in the schema :
const orderItemSchema = mongoose.Schema({
quantity: {
type: Number,
required: true
},
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
})
but you're passing an object into it here :
let newOrderItem = new OrderItem({
quantity: orderItem.quantity,
product: orderItem.product //<------ orderItem.product is an object
})
newOrderItem = await newOrderItem.save();
I think it should be :
let newOrderItem = new OrderItem({
quantity: orderItem.quantity,
product: orderItem.product.id // we only need the id of product
})
newOrderItem = await newOrderItem.save();
You may need to log orderItem.product before creating a new OrderItem to ensure the data inside it.
Basically I'm trying to get the time and the entity changed in a particular model when ever the update method is called.
This is my model I want to keep track of:
const mongoose = require("mongoose");
const modelSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
name: {
type: String,
required: true,
},
note1: String,
note2: String,
note3: String,
images: {
type: Array,
required: true
},
status: {
enum: ['draft', 'pending_quote', 'pendong_payment', 'in_production', 'in_repair', 'pemding_my_review', 'fulfilled'],
type: String,
default: "draft"
},
price: {
type: mongoose.Schema.Types.ObjectId,
ref: "Price",
}
}, {
timestamps: true,
})
module.exports = mongoose.model("Model", modelSchema)
And this is the method I call to update the status:
exports.updateModel = async (req, res) => {
try {
let id = req.params.id;
let response = await Model.findByIdAndUpdate(id, req.body, {
new: true
})
res.status(200).json({
status: "Success",
data: response
})
} catch (err) {
res.status(500).json({
error: err,
msg: "Something Went Wrong"
})
}
}
you can add a new field in your schema like:
logs:[{
entity: String,
timeStamp: Date
}]
Then updating it basing on your current code:
let id = req.params.id;
// I don't know whats in the req.body but assuming that it
// has the correct structure when passed from the front end
let response = await Model.findByIdAndUpdate(id,
{
$set:req.body,
$push:{logs:{entity:'your entity name here',timeStamp:new Date()}}
}, {
new: true
})
Blog Schema:
{
body: { type: String, required: true },
title: { type: String, required: true },
published: { type: String, default: false },
date: { type: Date, default: Date.now },
user: { type: Schema.Types.ObjectId, ref: 'BlogUser' },
comments: [{ type: Schema.Types.ObjectId, ref: 'Comments' }],
likes:[{user:{ type: Schema.Types.ObjectId, ref: 'BlogUser' }}]
}
Like Route for adding a like:
exports.likeBlog = async (req, res) => {
const blog_id = req.params.blog_id;
const user_id = req.body.user_id;
await Blog.findByIdAndUpdate(
blog_id,
{
$push: {
likes: {
user: user_id,
},
},
},
{ new: true },
(err, newBlog) => {
if (err) res.status(422).json(err);
console.log(newBlog);
res.json(newBlog);
}
);
};
Blog Route for reciveing a blog:
exports.getBlogByID = async (req, res) => {
const blog_id = req.params.blog_id;
try {
const blog = await Blog.findById(blog_id)
.populate("comments")
.populate("user");
console.log(blog);
res.json(blog);
} catch (error) {
res.status(401).json(error);
}
};
When I add a like by calling Like route from client, I get a blog with correct amount of likes i.e only 1. But when I request blog from Blog Route it returns me with two objects inside "likes" array, with both same as each other(same id too). Why am I getting such result? Mind you that I call 'Blog Route' after calling 'Like Route'.
It worked fine after I changed "like route" to this:
exports.likeBlog = async (req, res) => {
const blog_id = req.params.blog_id;
const user_id = req.body.user_id;
const blog = await Blog.findById(blog_id);
blog.likes.unshift({ user: user_id });
await blog.save();
Blog.findById(blog_id)
.then((result) => {
res.json(result);
})
.catch((error) => {
res.status(501).json({ error });
});
};
I still don't know what's the difference between the two though.