Issue while displaying fields of docs in MongoDB - javascript

I am new to programming and trying my best but without any help I don't think I am able to find it ;)
I want to display all products from mongoDB by brand. I already have route for category but in the same time when I am trying to implement the same strategy to brands I have 404 not found.
my code :
router.get('/:categorySlug', (req, res, next) => {
let filter = {};
if(req.query.hasOwnProperty("filter")){
filter['price'] = req.query.price
}
const slug = req.params.categorySlug;
Category.findOne({slug: slug})
.select('_id')
.exec()
.then(category => {
if(category){
if(category.parent === ""){
Product.find({category: category._id})
.select('_id name price productPic category brand slug')
.sort(filter)
.exec()
.then(products => {
res.status(200).json({
message: products
})
})
.catch(error => {
return res.status(404).json({
message: error
})
})
}
}else{
return res.status(404).json({
message: 'Not Found'
})
}
})
.catch(er => {
res.status(500).json({
error: er
});
});
router.get('/:brandSlug', (req, res, next) => {
const slug = req.params.brandSlug;
Brand.findOne({slug: slug})
.select('_id parent')
.exec()
.then(brand => {
if(brand){
Product.find({brand: brand._id})
.select('_id name price productPic brand slug')
.exec()
.then(products => {
res.status(200).json({
message: products
})
})
.catch(error => {
return res.status(404).json({
message: error
})
})
}else{
return res.status(404).json({
message: 'Not Found'
})
}
})
.catch(er => {
res.status(500).json({
error: er
});
});
Category, brand and product schema :
const brandSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, required: true, unique: true },
const categorySchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, unique: true },
const productSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true },
slug: { type: String, required: true, unique: true },
price: { type: Number, required: true },
oldPrice: { type: Number, default: 0 },
stock: { type: Number, required: true },
description: { type: String },
productPic: [
{
img: String
}
],
keyword: {type: String},
category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true },
brand: { type: mongoose.Schema.Types.ObjectId, ref: 'Brand', required: true }

You don't need to do two DB calls to get that required data, Instead you can join those two collections & get required data in one DB call ( Try to read about MongoDB's native $lookup or Mongoose's .populate() - which is a kind of wrapper to $lookup ).
But for now you can replace this :
Product.find({brand: brand._id})
with :
Product.find({brand: mongoose.Types.ObjectId(brand._id)})
Actual issue is brand._id is a string in code (In general when you read docs of brand collection in MongoDB & then log/print it or even use it in code You can see ObjectId()'s are converted to string's as ObjectId() is from BSON & not supported by JSON). So you need to convert input (brand._id) from string to ObjectId() before firing a query to product collection as brand field in product collection is of type ObjectId() !!
Note : Do not forget to import mongoose in this file otherwise it would fail that given point.

Related

How to save an array of strings in mongodb

I have found a few similar questions on stack overflow like this one:
How to save array of Strings in Node Mongodb
Mongoose - Save array of strings
but I cant figure out why my method is not working
I am trying to save the string of arrays "jobType".
context: I am creating an app where people can post jobs.
each job can have multiple types.
here is my job model::
const mongoose = require("mongoose");
const postSchema = mongoose.Schema({
content: { type: String, required: true },
imagePath: { type: String, required: true },
state: { type: String, required: true },
substate: { type: String, required: true },
address: { type: String, required: true },
jobType: { type: [String] },
creator: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true }
});
module.exports = mongoose.model("Job", postSchema);
this is the API used to save the data on MongoDB:
I am 100% sure that the data is getting to the API correctly.
the parameter "req.body.jobtype" contains all the info as a string.
I am trying to use JSON.parse to change the string into an array but its not working.
when I check MongoDB, an empty array is being stored
const Job = require("../models/job");
exports.createJob = (req, res, next) => {
console.log('create job api hit')
const url = req.protocol + "://" + req.get("host");
const post = new Job({
content: req.body.content,
imagePath: url + "/images/" + req.file.filename,
creator: req.userData.userId,
state: 'new',
substate: 'new',
address: req.body.address,
jobtype: JSON.parse(req.body.jobtype) // fix: not storing correctly
});
post
.save()
.then(createdJob => {
res.status(201).json({
message: "Job added successfully",
post: {
...createdJob,
'pecker':'pecker hecks out',
id: createdJob._id
}
});
})
.catch(error => {
res.status(500).json({
message: JSON.stringify(error)
});
});
};
You have a typo. In your model, you defined jobType property, but when saving the data, you are passing it as jobtype.
So, instead of this:
jobtype: JSON.parse(req.body.jobtype)
Do this:
jobType: JSON.parse(req.body.jobtype)

Mongoose filter by nested reference

I have 3 mongoose schemas Employee, Team and Project. Employee has reference to the team and Team has reference to the Project. Is it possible to get all employees by project Id? I don't want to change schema or use Team model with populate.
const employeeSchema = mongoose.Schema({
email: { type: String, required: true, unique: true },
team: { type: mongoose.Schema.Types.ObjectId, ref: "Team" },
});
const teamSchema = mongoose.Schema({
name: { type: String, required: true },
employees: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
project: { type: mongoose.Schema.Types.ObjectId, ref: "Project" },
});
Below code throws cast error, id is a valid project id.
router.get("/:id/employees", checkAuth, (req, res, next) => {
const id = req.params.id;
console.log(id);
Employee.find({ team:{project:id}}).then((employees) => {
console.log(employees);
});
});
Yes it is possible to get all employees using project Id.but not using single query so you have to modify your function like this
const id = mongoose.Types.ObjectId(req.params.id);
Team.findOne({ project: id }, { _id: 1 }, function (err, docs) {
// Get the Team which match with project ID
Employee.find({ team: docs._id }, function (err, docs1) {
//Get all employee belongs to that team and project
console.log(docs1);
});
});

How to push an object into an array with GraphQL?

I would like to change the way my resolver is creating the payment cards in my DB. So now I create the wallet in one collection then a payment in another collection and use the wallet_id to in my payment to link them. But now I want to push the payments into a cards[] that is defined in wallet. Any idea how to do that in resolver?
This is my Wallet schema
const WalletSchema = new Schema({
tokens: {
type: Number,
default: 0
},
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
unique: true
},
cards: [
{
type: Schema.Types.ObjectId,
ref: 'Payment'
}
]
}, { timestamps: true });
and this is my createPayment resolver
createPayment: async (_, { wallet_id, ...args }, { user }) => {
try {
await requireAuth(user);
const payment = await Payment.create({ ...args, wallet: wallet_id });
pubsub.publish(PAYMENT_ADDED, { [PAYMENT_ADDED]: payment });
return payment;
} catch (error) {
throw error;
}
},
Any idea?
You need to create another Schema for card payments;
const WalletSchema = new Schema({
tokens: {
type: Number,
default: 0
},
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
unique: true
},
cards: [ PaymentSchema ] <-- here
}, { timestamps: true });
// new schema
const PaymentSchema = new Schema({
type: Schema.Types.ObjectId,
ref: 'Payment'
});
Based on Wallet schema you need to write expose graphql schema

Search and filter data in Mongo DB using mongoose

var questionSchema = new Schema({
category: { name:
{
type: String,
lowercase: true,
required: [true, 'Category is a required field']
},
question:[
{
q: {
type: String,
lowercase: true
},
options: {
option1:{
type: String,
lowercase: true
},
option2:{
type: String,
lowercase: true
]
},
option3:{
type: String,
lowercase: true
},
option4:{
type: String,
lowercase: true
},
},
ans: {
type: String,
lowercase: true
},
level:{
type: String,
lowercase: true
}
}
]
},
},
{
strict: true,
runSettersOnQuery: true,
timestamps: {
createdAt: 'created', updatedAt: 'updated'
}
});
This is my question schema. I am trying to get all the questions of a particular category. Till now I have tried this ---
function (req,res,next) {
console.log(req.params.qcategory);
Question.find({
category:{
name: req.params.qcategory,
}
},'', function (err,data) {
if (err) {
err.status = 406;
return next(err);
}
console.log(data);
return res.status(201).json({
message: ' success.',data:data
})
})
};
In req.params.qcategory it contains the name of the category like 'programming' for instance.
But it returned me an empty array. **PS. There are questions of programming category in my DB. Then What am i doing wrong ???
Also is there a way to first check what category a question is and then add to the question array inside that particular category. I don't want to add questions with categories again and again instead i want to check if that category already exists in the database and push the question inside the array of that question category. For instance if a question is of 'programming' category and in the database there is already a question which is of again a 'programming' category then push the question inside the array of programming category.
If my questions are not clear then please comment I will respond quickly.
Try this, Not tested
function (req,res,next) {
console.log(req.params.qcategory);
Question.find({
"category.name": req.params.qcategory,}
, function (err,data) {
if (err) {
err.status = 406;
return next(err);
}
console.log(data);
return res.status(201).json({
message: ' success.',data:data
})
})
};
Replace the
{category:{name:req.params.qcategory}} with {"category.name":req.params.qcategory}.
read more on mongodb's Query Embedded Documents https://docs.mongodb.com/.../method/db.collection.find/
Try this.
function (req,res,next) {
console.log(req.params.qcategory);
const name = req.params.qcategory;
Question.find({name})
.then((data)=>return res.status(201).json({message:"success",data:data})
.catch(err=> {
res.status(406)
return next(err);
})
};

Connect mongoose-array-values to a unique ID

This may seem like a vague question, but I'm going to try to explain the best I can. As a side note, I'm quite new to using mongoose :)
I have a mongoose-schema storing different values for each user, like so...
let userSchema = mongoose.Schema({
user: { type: String, required: true, unique: true },
pass: { type: String, required: true },
files: [{ type: String, required: false }],
});
The "files"-key contains an array of values, lets say for example:
userSchema.files = [value1, value2, value3]
And I want each value to be connected to some kind of ID, so that when I call the specified ID, I get the specified value. Just for demonstrating purposes, it could look something like this:
userSchema.files = [{value:value1, id: id1},
{value:value2, id: id2},
{value:value3, id: id3}]
Then I want to find the specified id, and return it's "value"-key in a request:
router.route("/home/:id")
.get(restrict, function(req, res) {
User.findOne({ user: req.session.Auth.username }, function(error, data) {
data.files.forEach(function(file) {
if (file.id === req.params.id) {
response.render("../home", file.value)
}
}
});
});
How can I do this? Tried pushing an object to files, but that didn't work as expected. Read something about ObjectId, but couldn't quite understand it. Any tips?
I think you simply need to create a separate model for File and connect it to your User model using the 'ref' keyword :
let fileSchema = mongoose.Schema({
_id : Number,
value : String
});
let userSchema = mongoose.Schema({
user: { type: String, required: true, unique: true },
pass: { type: String, required: true },
files: [{ type: Number, ref: 'File' }]
});
let User = mongoose.model('User', userSchema);
let File = mongoose.model('File', fileSchema);
let f1 = new File({ _id: 1, value: 'File 1'});
let f2 = new File({ _id: 2, value: 'File 2'});
let f3 = new File({ _id: 3, value: 'File 3'});
let user1 = new User({user:'chuck', pass:'norris'});
user1.files.push(f1);
user1.files.push(f2);
user1.files.push(f3);
user1.save(function(err){ });
Now to get the data back:
User
.findOne({ user: 'chuck' })
.populate('files') // only works if we pushed refs to children
.exec(function (err, user) {
if (err) return handleError(err);
console.log(user);
//you can now loop through user.files and compare _id
user.files.forEach(function(file) {
if (file._id === req.params.id) {
response.render("../home", file.value)
}
}
});
You can read about mongoose reference population here: http://mongoosejs.com/docs/populate.html

Categories