I have an app which displays just movies from external api, so I have added comment section to my app for single movie, my problem is when a user enter a comment in a movie called "TITANIC" I can see the comment added to all movies This is wrong, what I want is comment added to a movie TITANIC should not be displayed to other movies.
Here is what I have:
server.js UPDATE
router.post('/comments', function(req, res) {
var comment = new Comments(req.body);
comment.save()
.then(item => {
res.status(200).json({'comment': 'comment added successfully'});
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
service.js
// Adds comments
addReview(author, description): Observable<any> {
const uri = 'http://localhost:8000/movies/comments/';
const obj = {
author: author,
description: description
};
return this.http.post(uri, obj);
}
// Return Comments
getComments(id: string): Observable<any> {
const url = `${apiUrl + this.commentsUrl}/${id}`;
return this.http.get(url, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError));
}
component.ts
addReview(author, description) {
this.moviesService.addReview(author, description).subscribe(success => {
this.flashMessages.show('You are data we succesfully submitted', { cssClass: 'alert-success', timeout: 3000 });
// get the id
this.activeRouter.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getComments(id)
.subscribe(comments => {
console.log(comments);
this.comments = comments;
});
});
}, error => {
this.flashMessages.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 });
});
}
Question
What am I missing in my code?
You should /post to a specific movie ID where you add the new comment under its comments field array for example.
Also, having /comments is a bad practice since what you really want is posting to a specific movie with its attached comment. Something like the following:
router.post('/movie/:id/newcomment:', function(req, res) {
Movie.findOne({ _id: req.params.id })
.then(movie => {
movie.comments.push(req.body.comment);
movie.save();
.then(result => {
res.status(200).json({'message': 'comment added successfully'});})
.catch(err => {
res.status(400).send("unable to save to database");
});
}
});
But the main problem here is that you misunderstand what exactly you need to do.
You fetch data from an existing API, yet you want to add specific comments to specific movies from the fetched data.
You cannot achieve it without having full control on your external API.
What you could actually do is build a client side data structure to hold all the fetched data, and then add to each movie object inside that data structure another property which will be named comments and an ID property, so you can later access that data structure by ID and update a specific property.
1) Add the corresponding movie id to the POST request adding a new comment.
addReview(movieId, author, description): Observable<any> {
const obj = {movieId, author, description};
2) When you request the comments for a movie by its id, use the movieId in your database query (depends on your database and ORM)
If you're using mongodb and mongoose, it'd be something like:
router.get('/comments/:movieId', function(req, res) {
const movieId= request.params.movieId;
Comments.find({ movieId });
Related
How can i display newest user post in my app? i have a backend route which display user post but i want that route display latest post of user So how can i do that in my code?
My code:
router.get('/postdata', async (req, res) => {
try {
// Find all users in the database
const users = await User.find();
// Map over the users array and return an array of objects
// with the same username, profile_image, and postImage
const userData = users.flatMap(user => {
return user.posts.map(post => ({
username: user.username,
profile_image: user.profilepic,
postImage: post.post,
}));
});
return res.json(userData);
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
If your posts model has created_at or updated_at properties that keep track of when an image was uploaded, you could use that to sort the array in your map.
Let's say your userData array has similar output to this.
[
{
username: 'user1',
profile_image: 'https://your_domain.com/user1-profile.jpg',
postImage: 'https://your_domain.com/user1-post1.jpg',
created_at: '2023-01-01T11:00:00.000
},
{
username: 'user2',
profile_image: 'https://your_domain.com/user2-profile.jpg',
postImage: 'https://your_domain.com/user2-post1.jpg',
created_at: '2023-01-01T12:00:00.000
}
]
Then you can sort the array before rendering it.
const sorteduserData = userData.sort((a, b) => {
return new Date(b.created_at) - new Date(a.created_at);
});
It's a good practice to have your backend do the sort to reduce overhead on the front-end and to have your application load faster.
Many of headless CMSs have these features built in.
I have a User Model and recently added some key to it. This means that existing users will not have this key initially and new users do. Now I have a route where I want to check if the particular key exists on the user object so that I can add it if it returns false.
This is my route currently:
router.post("/new-application", verifyUser, (req, res) => {
const { application } = req.body;
User.findById(req.userId)
.then((user) => {
if (user.hasOwnProperty("applications")) {
console.log("has applications");
} else {
console.log("has not applications");
user["applications"] = initialApplications;
}
user.save().then((updatedUser) => {
// console.log(updatedUser);
});
})
.catch((err) => {
console.log("err fetching user: ", err);
res.end();
});
});
The problem is that if (user.hasOwnProperty("applications")) always returns false even after I added it to the user. I also tried if("applications" in user). That also does not work.
So how can I check if a key or field exists on a Mongoose object.
A simple way of checking if the field exists or not can be done by $exist.
router.post("/new-application", verifyUser, (req, res) => {
const { application } = req.body;
User.findById({$and: [{_id: req.userId}, {applications: {$exists:false}}]})
.then((user) => {
// it will return the user only when
// applications doesn't exist
})
.catch((err) => {
console.log("err fetching user: ", err);
res.end();
});
});
Note: the reason your old user doesn't show the applications is they don't have it when you saved them and changing the model now won't add this property to old models. So, we can use the $exists operator to check it.
I have read all sorts of variations of this on stackoverflow but I cannot seem to find a post that exactly explains what I'm trying to achieve, at the same time I believe this has to be a very common task during saving data.
So I need to save data to one collection and then read the _id from that doc and save it to a doc in a different collection. I have the following code and I can see the correct data with console.log but I don't see the data being saved to the database.
Appreciate if someone can guide me in the right direction.
Thank you!
router.post('/signup', async (req, res) => {
const { email, password, name, country } = req.body;
try {
const user = new User({ email, password });
await user.save((error, doc) => {
if (error) {
console.log(error);
} else {
const userProfile = new UserProfile({ userId: doc._id, name, country });
userProfile.save((error, doc) => {
if (error) {
console.log(error)
} else {
console.log(doc) // Can see this log with the correct data
}
});
}
});
const token = jwt.sign({userId: user._id}, 'MY_KEY');
res.send({ token });
} catch(error) {
return res.status(422).send(error.message)
}
})
I want to get updated table values after I add user to my "WOD" table. For instance, I have 2 users in my WOD table and after I add third user , I want to return a response to client with I have just inserted data (third guy). But now , I can only return first 2 users because I can not take updated values. Of course I can make another query to get updated table values after I insert, but is there any better solution ? Here is my codes;
const addUser = async (req, res) => {
try {
const { userId, wodId } = req.body;
if (!userId || !wodId) {
res.status(400).send({ status: false, message: 'need userId and wodId' });
}
const wod = await Wod.findByPk(wodId, {
include: [
{
model: User,
as: 'Participants',
through: { attributes: [] }
}
]
});
//check capacity if full.
if (wod.Participants.length >= wod.capacity) {
res
.status(403)
.send({ status: false, message: 'Capacity of this class is full!' });
}
const result = await wod.addParticipants(userId);
res.status(201).json({ status: !!result, wod });
} catch (error) {
res.status(500).send({ status: result, message: error.message });
console.log(error.message);
}
};
As a result of many-to-many association sequelize.sync will generate some functions for us. You are used addParticipants function and this returns an array that added to the assocation(userwod) table.
In this array you will find some id fields(join table fields) because you just run like this INSERT INTO 'user_wods' ('user_id''wod_id') VALUES (2,1). If you want to return the added user's information then you should run a SELECT * FROM 'user' WHERE 'id'=2.
You must call reload function for fetch the third guy.
await wod.reload()
I understand that there is no built-in way in Sails.js/Waterline of populating deep nested associations yet, so I am trying to use bluebird promises to accomplish that but I'm running into a problem.
I'm successfully retrieving the user, and all the posts (populated with the images collection) associated with it (console.log shows me that everything is filled properly). However, when I override the property "post" of the user and try to assign the fully populated posts retrieved before, it does not fill properly the images property of Post.js. It is like the ORM is preventing the image collection of Post.js to be manually assigned.
What am I doing wrong? What is the best way of populating deep nested one-to-many associations?
Bellow I've pasted all the code that I'm executing....
// Populate nested association
nested: function (req, res, next){
var username = req.param("id");
User
.findOneByUsername(username)
.populateAll()
.then(function (user){
var posts = Post.find({
"user": user.id
})
.populate('images')
.populate('category')
.then(function (posts){
return posts;
});
return [user, posts];
})
.spread(function (user, posts){
user.posts = posts; // This won't work.... It assigns all the fields properly but the images collection attribute
res.json(user);
}).catch(function (err){
if (err) return res.serverError(err);
});
}
// --- User.js Model --- //
module.exports = {
attributes: {
.....,
posts: {
collection: "post",
via: "user"
},
.....
}
}
// --- Post.js Model --- //
module.exports = {
attributes: {
....,
user: {
model: "user"
},
images: {
collection: "postImage",
via: "post"
},
....
}
}
// --- PostImage.js Model --- //
module.exports = {
attributes: {
....,
post: {
model: "post"
}
},
}
Regards,
Sávio Lucena
This might be an old question, but its better to have an answer, so sails.js users can benefit of it.
Your issue here is that when sails returns a record (Inside an array), the keys of that record that correspond to associations, are in fact getters/setters, and it seems that the setter does not allows what you want. You can use Object.getOwnPropertyDescriptor(user, 'posts') to confirm.
So what you need to do in order to be able to override that property as you want, is to call .toObject on it, (or clone its properties via _.clone or manually looping but you'll get a lot of junk with it, so stick to the .toObject), in any case you get a new object with the properties you need, and there is no restriction in how you modify it now.
So your code will look like this:
User
.findOneByUsername(username)
.populateAll()
.then(function (user){
var posts = Post.find({
"user": user.id
})
.populate('images')
.populate('category')
.then(function (posts){
return posts;
});
return [user, posts];
})
.spread(function (user, posts){
user = user.toObject() // <- HERE IS THE CHANGE!
user.posts = posts; // It will work now
res.json(user);
}).catch(function (err){
if (err) return res.serverError(err);
});
}
You have to overwrite each post id object in user.posts array. For more info check this answer https://stackoverflow.com/a/26452990/4261327.