Hi friends I'm trying to find in my subDoc category string matching
Here is the code:
router.get('/:_categoryName', (req, res, next) => {
Malgezot.findOne({ 'items.category': req.params._categoryName }, (err, malgezot) => {
if(err) return res.render('body/category', {info: ''});
console.log(malgezot);
});
});
But the results is all of the items!
I also tried:
router.get('/:_categoryName', (req, res, next) => {
Malgezot.find({'items': { 'category': req.params.categoryName }}, (err, malgezot) => {
if(err) return res.render('body/category', {info: ''});
console.log(malgezot);
});
});
If your data is in form of object then query should be :
router.get('/:_categoryName', (req, res) => {
const { _categoryName } = req.params;
Malgezot.findOne({
'items.category': _categoryName
}).then((data) => {
if (data) {
res.status(200).json(data)
}
}).catch((err) => {
res.status(500).json({
message: 'Internal server error'
});
});
});
Or if your data in form of array then your query should be:
router.get('/:_categoryName', (req, res) => {
const { _categoryName } = req.params;
Malgezot.findOne({
item : { $in : [{ category: _categoryName }] }
}).then((data) => {
if (data) {
res.status(200).json(data)
}
}).catch((err) => {
res.status(500).json({
message: 'Internal server error'
});
});
});
Related
Function that i want to test:
const homepageGet = (req, res, next) => {
Product.find(function (err, products) {
if (err) {
console.log(err);
next(err);
} else {
res.render('allProducts', { title: res.__('allProducts.title'), response: res, products: products });
}
});
};
I want to test if this function is actually sending products to the view. This is the test i wrote:
describe('Home page with all products', () => {
it('shows all products', (done) => {
chai.request(server)
.get('/')
.end((err, response) => {
response.body.should.have.property('products');
done();
});
});
});
This test fails and shows the following error message:
Uncaught AssertionError: expected {} to have property 'products'
How do I fix this?
I've got this function that calls a get request:
confirmUser(username: string) {
this.http.get<{ users: User[] }>('http://localhost:3000/users').subscribe(
// success function
(response) => {
this.user = response.users;
console.log(this.user);
}
),
(error: any) => {
console.log(error);
}
}
How can I make it so that the username being passed in in the above function be available to replace the username Grok here:
router.get('/', (req, res, next) => {
// User.find().then(users => {
User.findOne({username: "Grok"}, {} ).then(users => {
res.status(200).json({
users: users
});
})
.catch(error => {
returnError(res, error);
});
});
The value of username is coming from a form field, so the component calling this is not subscribed to any URL parameters, if that makes a difference.
if you pass it as a parameter you should use req.params, otherwise req.query for querystring.
using param (i.e.: http://localhost:3000/users/grok):
router.get('/:username', (req, res, next) => {
// User.find().then(users => {
User.findOne({username: req.params.username }, {} ).then(users => {
res.status(200).json({
users: users
});
})
.catch(error => {
returnError(res, error);
});
});
using querystring (i.e.: http://localhost:3000/users?username=grok) :
router.get('/', (req, res, next) => {
// User.find().then(users => {
User.findOne({username: req.query.username }, {} ).then(users => {
res.status(200).json({
users: users
});
})
.catch(error => {
returnError(res, error);
});
});
product-operations.component.ts
deleteProduct() {
this.productsService.delete_product(this.deleteID).subscribe((res: any) => {
console.log("helloooooo");
});
};
product.service.ts
delete_product(id) {
return this.http.delete("http://localhost:3000/delete_product/" + id);
}
backend
exports.deleteProduct = (req, res, next) => {
const id = req.param("id");
Product.deleteOne({ _id: id })
.then(() => {
console.log("deleted");
})
.catch(err => {
console.log(err);
});
};
Problem:
In the above codes, the deleteProduct function in product-operations.component.ts doesn't work properly. More precisely, it does the removal. But after doing the uninstall, subscribe doesn't run its contents. This prevents my instant update after deletion. How can I solve this?
Try to send a response back from the server.
exports.deleteProduct = (req, res, next) => {
const id = req.param("id");
Product.deleteOne({ _id: id })
.then(() => {
res.send({}) // or res.send({id: id})
console.log("deleted");
})
.catch(err => {
res.status(500)
res.send({error: err})
console.log(err);
});
};
I want to exclude default _id or any one of the two fields (time and count) while fetching the data result from JSON API in my node-express app. I tried several ways to do so but still the data shows the field that I marked as excluded in GET response. In my code below, I used _Id as sample, but similar problem persists for every field of my database. Here are some of the ways I figured out that didn't worked:
exports.get_all_clothing = (req, res, next) => {
Clothing.find({}, {projection:{_id: 0}})
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
AND
exports.get_all_clothing = (req, res, next) => {
Clothing
.find({}).project({ _id: 0 })
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
AND
exports.get_all_clothing = (req, res, next) => {
Clothing.find('-_id')
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
Everytime I keep getting the result as
[ { _id: 5cb73
time: '656
count: 43,
__v: 0 },
{ _id: 5cb73
time: '155
count: 60,
__v: 0 },
{ _id: 5cb73
time: '155
count: 56,
__v: 0 }, ....]
Please help out.
Try this:
exports.get_all_tweets = (req, res, next) => {
Clothing.find({}, {_id: 0})
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
You can use query.select to get your desired results.
exports.get_all_tweets = (req, res, next) => {
Clothing.find()
.select('-_id')
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
In the client side I have :
handleChangeSave(values, id) {
axios.post('/api/users/change' + id, JSON.stringify(values))
.then(resp => {
console.log(resp.data);
})
.catch(function (error) {
console.log(error);
});
}
On the server side I am trying to query for the object and it is coming back empty when it is really not.
router.post('/users/change:id', (req, res) => {
User.findById({ '_id': req.params.id }, (err, user) => {
if(err) return res.status(500).send(err);
let Values = req.query;
console.log(Values); // returns {}
})
});