How to create function edit objects in object array in Node.js - javascript

I want to create a patch or put a function for edit values of the object array.
comments: [{
body: String,
date: Date,
id: String
}],
router.put('/comment/:id', (req, res) => {
const commentbody = req.body.body
Blog.findOneAndUpdate({ "_id": req.params.id }, { $set: { comments: { body: commentbody } } }).then(result => {
res.send('comment edited');
})
.catch(err => res.send(err))
})

Related

Api call finds already deleted documents

I've two endpoints: one that delete product document and one that retrieve the document.
After I delete the document throught by Id, the GET api call return me already the document even if it's deleted and It's not present on MongoDb.
Response of DELETE call returns { deletedCount: 1 }
Here code of GET product:
exports.getSingleProduct = (req, res, next) => {
let id = req.params.id;
Products.findById(id).populate({ path: 'internalColor' }).then(result => {
if(result && result.visible == true) {
res.status(200).json(result)
} else {
res.status(404).json({
message: 'product_not_found',
id: id
})
}
}).catch(err => {
res.status(404).json({
message: 'error_operation: ' + err,
id: id
})
});
}
Here code of DELETE product:
exports.deleteDefinallySingleProduct = (req, res, next) => {
let id = req.params.id;
console.log(id)
Products.deleteOne({ id: id }).then(result => {
if(result) {
res.status(200).json({
message: 'deleted_product'
});
}
}).catch(err => {
res.status(404).json({
message: 'error_operation: ' + err
})
})
}
Products Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const options = {
timestamps: true
}
const productSchema = new Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
externalUrl: {
type: String,
required: true
},
imgUrl: {
type: String,
required: true
},
brand: {
type: String,
required: true
},
visible: {
type: Boolean,
required: true
}
}, options);
const Product = mongoose.model('Products', productSchema);
module.exports = Product;
I think the error that you are facing is caused by a typo in your code.
exports.deleteDefinallySingleProduct = (req, res, next) => {
...
Products.deleteOne({ id: id }).then(result => {
if(result) {
// this code will run always
console.log(result) // will show { n: 0, ok: 1, deletedCount: 0 },
// That is, this section of code will run always despite of delete count being 0 or more due to the request to be excecuted successfully.
...
}
The correct implementation is here:
exports.deleteDefinallySingleProduct = (req, res, next) => {
...
Products.deleteOne({ _id: id }).then(result => {
...
}
This is because by default mongooose use _id representing the document id, unless create a custom id in the schema which you didn't do.

Firebase SnapShot.val() returns null when trying to access data in vuex

I'm Creating an Application where student, staff and non-teaching staff can access.
my Form Data looks like this:
formData: {
name: "",
email: "",
password: "",
select: null
},
options: ["Student", "Staff", "Non-Teaching Staff"],
Of course in Vuex store i can register user with:
registerUsers({}, payload) {
firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
.then(res => {
const userId = firebaseAuth.currentUser.uid;
console.log(res)
Notify.create({
message: 'Regsitration Successful!.',
color: 'primary',
classes: 'quick'
})
//set student
firebaseDb.ref(`users/'${userId}`).set({
name: payload.name,
email: payload.email,
select: payload.select
});
})
.catch(err => {
console.log(err)
Notify.create({
message: `${err.message}`,
classes: 'quick',
color: 'negative'
})
})
I can also loginUsers with:
loginUsers({}, payload) {
firebaseAuth.signInWithEmailAndPassword(payload.email, payload.password)
.then(res => {
console.log(res);
Notify.create({
message: 'Success!',
classes: 'quick',
color: 'positive'
})
})
.catch(err => {
console.log();
Notify.create({
message: `${err.message}`,
classes: 'quick',
color: 'negative'
})
})
},
The Probems comes from this :
handleAuthStateChange() {
firebaseAuth.onAuthStateChanged(user => {
if (user) {
//set Student
const studentId = firebaseAuth.currentUser.uid;
console.log(studentId)
firebaseDb.ref(`users/${studentId}`).once('value', snapshot => {
console.log(snapshot.val())
})
}
})
},
The Snapshot.val() return null in the console.
What i'm i writing wrong please.
It seems that, by calling firebaseDb.ref(`users/'${userId}`).set({...}) you are creating your user under a node
users/'userId
with a single quote (').
And you try to read the node
users/userId
which does not exists, if the assumption that you mistakenly added a single quote is right.
In addition note that you don't need to do
firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
.then(res => {
const userId = firebaseAuth.currentUser.uid;
//...
because createUserWithEmailAndPassword() returns a UserCredential. So you can do:
firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
.then(res => {
const userId = res.user.uid;
//...
and also that you can do:
handleAuthStateChange() {
firebaseAuth.onAuthStateChanged(user => {
if (user) {
const studentId = user.uid;
//......

Count number documents from collection inside Array

I'm trying to count the number of documents inside an array from a collection.
Below you can see the Schema.
MongoDB Schema
What I want to count is each type of expenseType but since I have this value inside an array I don't know how to build a query to get this value.
The final result should be:
Water: 2 | Wifi: 1
And when I add new Water should be Water:3 and so on.
Below I show what I'm trying to do, but received an error
router.get("/getExpense", ensureAuthenticated, (req, res) => {
House.aggregate(
{
$match: {
userID: req.user.id,
expensesHouse: { $elemMatch: { status: "Public" } }
}
}
{ $group: { _id: "$Water", price: { $sum: 1 } } }
).then(house => {
console.log(res.json({ house }));
});
});
The res.json is because I send a JSON with the values and fetching to build a chart.
This is the fetch I'm doing.
getData();
async function getData() {
const res = await fetch("/houses/getExpense", {
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
})
.then(res => res.json())
.then(data => {
console.log(data);
});
}
You're looking for $unwind. It creates intermediary entries that you can group on.
House.aggregate(
{
$match: {
userID: req.user.id,
expensesHouse: { $elemMatch: { status: "Public" } }
}
},
{
$unwind: '$expensesHouse',
},
{ $group: { _id: "$expensesHouse.expenseType", price: { $sum: 1 } } }
).then(house => {
console.log(res.json({ house }));
});
I've also fixed the last group id.

DynamoDB: ValidationException: The provided key element does not match the schema

I'm getting the error ValidationException: The provided key element does not match the schema trying to find the field username in my database, when querying the id manually I get a response but with the username I get the error.
const readKey = async (table, queryData, column) => {
const params = {
TableName: table,
Key: {},
}
// { TableName: 'users', Key: { username: 'Maarten' } }
params.Key[column] = queryData
return await new Promise((res, rej) => {
dynamodb.get(params, (err, data) => {
if (err) {
rej(err)
console.log(err)
}
console.log(data)
res(data)
})
})
}
The response:
message: 'The provided key element does not match the schema',
code: 'ValidationException',
time: 2019-11-24T23:50:37.472Z,
requestId: 'RHQF02LFVE7V3NNSGDBF8JR97RVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 400,
retryable: false,
retryDelay: 14.742327788838905
when I change the params to this:
const params = {
TableName: table,
Key: {
id: '22601770-37ea-47ce-9814-bd979ca4c841',
},
}
I get the response:
{
Item: {
id: '22601770-37ea-47ce-9814-bd979ca4c841',
password: '$2b$10$f/gXRrSCCALPRedCIxOwVuhzi2EX07DeEDVF4h10UuUN.DgYY2Hnq',
username: 'Maarten'
}
}
What am I doing wrong?

mongodb/mongoose findOneandUpdate how to get index and delete object

So I am having event object, which have comments, and comments have likes array.
What I currently can do is to add like to comments array of event object.
My schema looks similar to this:
creator: {
type: Schema.Types.ObjectId,
ref: 'User'
},
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
text: {
type: String,
required: true
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}
]
}
]
}
And my current add like to comments function looks like this:
commentLike: async (req, res) => {
console.log('working', req.params.id, req.params.idas, req.params.commentID);
Events.findOneAndUpdate(
{ _id: req.params.idas, comments: { $elemMatch: { _id: req.params.commentID } } },
{ $push: { 'comments.$.likes': { user: req.params.id } } },
(result) => {
res.json(result);
}
);
}
Params: idas- event._id, commentID: comment id, id: user._id
The problem is that i can add endless likes since, I have no logical operation to check if user already liked it, and im really strugling, in this findoneandupdate function to do that. But thats on problem, another thing what I want to do is unlike comment, and Im having atrouble at figuring it out, on how to get user index from likes array so i can slice that index out, currently my function is looking like this:
deleteLike: async (req, res) => {
console.log('working', req.params.id, req.params.idas, req.params.commentID);
Events.findOneAndUpdate(
{ _id: req.params.idas, comments: { $elemMatch: { _id: req.params.commentID } } },
{
$push: {
'comments.$.likes': {
$each: [],
$slice: 0 //there instead of 0 should be user index
}
}
},
(result) => {
res.json(result);
}
);
}
On this function im also using findoneandupdate function, which is probably not a good idea? Was trying to use findandremove, but it removes entire event object.
So i managed to to it, by using pull operator.
Working delete comment like fuction
deleteLike: async (req, res) => {
console.log('working', req.params.id, req.params.idas, req.params.commentID);
Events.findOneAndUpdate(
{ _id: req.params.idas, comments: { $elemMatch: { _id: req.params.commentID } } },
{
$pull: { 'comments.$.likes': { user: req.params.id } }
},
(result) => {
res.json(result);
}
);
}
};

Categories

Resources