I want to add multiple comments to my Article (mongoose) - javascript

I am creating a website and I want to seed some data.
I want to create two articles with two comments each :
var mongoose = require("mongoose"),
Article = require("./models/articles.js"),
Comment = require("./models/comments.js");
var articleData = [
{
title: "Fast Cars",
author: "Steve Novak",
company: "AthLead",
body: "SOME TEXT"
},
{
title: "New Design",
author: "Kevin Garnett",
company: "Revoos",
body: "COOL ARTICLE"
}
];
var commentData = [
{
body: "I really love the new design",
author: "Mark Cuban"
},
{
body: "This is hitting my funny bone",
author: "Lisa Jones"
}
];
I am Deleting all past comments and articles.
Then for each article I am first creating the article and then pushing two comments.
I am getting a problem at saving the newly formed article to the database :
Placing article.save() at 1. causes parallel save() error to occur
Whereas placing at 2. causes no comment to be saved.
function seedDB() {
Comment.deleteMany({}, function (err) {
if (err) {
console.log(err);
}
Article.deleteMany({}, function (err) {
if (err) {
console.log(err);
}
articleData.forEach(function (article) {
Article.create(article, function (err, article) {
if (err) {
console.log(err);
} else {
commentData.forEach(function(comment) {
Comment.create(comment, function (err, comment) {
if (err)
console.log(err);
else {
article.comments.push(comment);
// PROBLEM
// 1. article.save();
}
})
})
// PROBLEM
//2. article.save();
}
});
});
});
});
});
console.log("Database Reset");
}
module.exports = seedDB;

You are currently in callback hell and using async/await willl go a long way making your code readable and thus makes it easy to debug.
Consider the following workflow that uses async/await
async function seedDB() {
try {
// clean up all comments
await Comment.deleteMany({}).exec()
// clean up all articles
await Article.deleteMany({}).exec()
// create articles
await Article.create(articleData)
// create comments
const comments = await Comment.create(commentData)
// update articles with the new comments
const updatedArticles = await Article.updateMany(
{},
{ '$set': { comments } }
).exec()
console.log(updatedArticles)
} catch (err) {
console.error(err)
}
}

You can pass an array of documents to Model.create, just create comments first then use Array.map to create a new array from articletData with comments assigned to each article
using async/await:
async function seedDB() {
...
const comments = await Comment.create(commentData);
const articles = await Article.create(articleData.map(article => { article.comments = comments; return article }));
...
using Promise.then():
function seedDB() {
...
Comment.create(commentData).then(comments =>
Article.create(articleData.map(article => { article.comments = comments; return article })).then(articles => {
});
});
...

When you first Article.create..., on successfull creation the function returns the article object or a error on the other side.
You need to run the forEach loop in the newly created article for achieve each article contain the Comments.
you need to work with the existed newly created article data.
Example
articleData.forEach(function (article) {
Article.create(article, function (err, article) {
if (err) {
console.log(err);
} else {
article.update(
{ "_id": ObjectId(article.id) },
{ $push: { comments: commentData } }
);
// OR try something like
// article.comments = commentData
// OR
// article.comments.insert(commentData)
article.save();
}
});
});

Related

Error in updating profile with image using mongoose and cloudinary

updateProfile: async function(req, res) {
try {
const update = req.body;
const id = req.params.id;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
const image = req.files.profileImage;
const cloudFile = await upload(image.tempFilePath);
const profileImage = cloudFile.url
console.log('Loging cloudfile', profileImage)
await User.updateOne(id, { update }, { profileImage }, { new: true },
function(err, doc) {
if (err) {
console.log(err)
}
if (doc) {
return res.status(200).send({ sucess: true, msg: 'Profile updated successful' })
}
});
} catch (error) {
res.status(500).json({ msg: error.message });
}
}
But I'm getting an error of "Callback must be a function, got [object Object]"
I have tried to $set: update and $set: profileImage but still not working.
So the image successful upload into the cloudinary but the update for mongoose is not working.
Upon brief research into the issue, I think you are feeding the arguments in wrong. Objects can be confusing but not to worry.
Your code is:
await User.updateOne(id, { update }, { profileImage }, { new: true }
However, I believe it should be something more like:
await User.updateOne({id: id}, { profileImagine: profileImage, new: true },
The API reference annotates use of the function as:
const filter = { name: 'John Doe' };
const update = { age: 30 };
const oldDocument = await User.updateOne(filter, update);
oldDocument.n; // Number of documents matched
oldDocument.nModified; // Number of documents modified

How to access variable in a function expression nodejs [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
Is there a way to get the contents of variable data outside the function. I am so stuck, looked various forums in stackoverflow and but failed. I am trying to retrieve data from mongoose model and push those results into an array results and when i print results array, i get empty array .
var results = []
Model.find({firstName:name}, function(err,data){
if(err)
throw err;
data.forEach(function(element) {
console.log(element);
results.push(element);
});
});
console.log(results) --> []
But when i try to print the data inside the ForEach, I am was get the results, listed below.
0 => { _id: 5dc9953a2168993711903698,
id: 763,
firstName: 'Deepak',
lastName: 'Kalra',
image_id: 'No',
logged: false,
__v: 0
}
1 => {
_id: 5dc995546f0f88372080ea36,
id: 511,
firstName: 'Deepak',
lastName: 'Kalra',
image_id: 'No',
logged: false,
__v: 0
}
Entire code
alexa.intent("FirstName", {
"slots": { "name": "AMAZON.FIRST_NAME" },
"utterances": [
"sure {-|name}","{-|name}","my name is {-|name}"
]
},
function(request, response) {
var name = 'Deepak';
try {
var results = await Model.find({firstName:name});
console.log(results)
} catch (error) {
// Handle error.
}
// Model.find({firstName:name}, function(err,data){
// if(err)
// throw err;
// data.forEach(function(element) {
// console.log(element);
// results.push(element);
// });
// });
console.log(results);
});
Is there any solution to fix it. please help me
Because, console.log(results) executed before Model.find was finished.
Two things you can do here:
Put console.log(results) inside the callback.
Use async/await to get similar behaviour.
Example (callback):
Model.find({firstName:name}, function(err,data){
if(err)
throw err;
console.log(data); // data is already an array
});
Example (async/await):
try {
var results = await Model.find({ firstName: name });
console.log(results)
} catch (error) {
// Handle error.
}
Model.find already returns an array of document, so you don't have to run a loop to push them into an array.
UPDATED
alexa.intent("FirstName", {
"slots": { "name": "AMAZON.FIRST_NAME" },
"utterances": [
"sure {-|name}", "{-|name}", "my name is {-|name}"
]
},
async function (request, response) {
var name = 'Deepak';
try {
var results = await Model.find({ firstName: name });
console.log(results)
} catch (error) {
// Handle error.
}
});
Notice the async in front of function.

How to return a list of SQS queues in a module exports function?

I'm very new to node.js so I think I'm missing something obvious here.
I'm simply trying to get a list of SQS queues using aws-sdk and return them from a module to be accessible to other code. list_queues is the function in question.
The code below works to an extent, I see a "success" log and a log of a string array of all my queues, however, the function does not return that array to the caller and I don't understand why.
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
var sqs;
var sts = new AWS.STS();
sts.assumeRole({
RoleArn: 'arn:aws:iam::xxxxx:role/UserRole',
RoleSessionName: 'NodeDeveloperRoleSession'
}, function(err, data) {
if (err) { // an error occurred
console.log('Cannot assume role :(');
console.log(err, err.stack);
} else { // successful response
console.log('Assumed role success :)');
AWS.config.update({
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
sessionToken: data.Credentials.SessionToken
});
sqs = new AWS.SQS({apiVersion: '2012-11-05'});
}
});
exports.list_queues = function() {
sqs.listQueues({}, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("success");
console.log(data.QueueUrls);
return data.QueueUrls;
}
});
}
Any help is appreciated
exports.list_queues = function() { // 2. but you actually want to return from this one
sqs.listQueues({}, function(err, data) { <-----------------
if (err) { |
console.log("Error", err); |
} else { |
console.log("success"); |
console.log(data.QueueUrls); |
return data.QueueUrls; // 1. you are returning from this one
}
});
}
there are two ways you can make it work
Promise based
exports.list_queues = function() {
return sqs.listQueues({}).promise().then((data) => data.QueueUrls);
}
// and in another file you would:
const {list_queues} = require('./list_queues.js');
list_queues.then((queues) => console.log(queues));
Callback based
exports.list_queues = function(cb) { // notice I added callback here
sqs.listQueues({}, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("success");
console.log(data.QueueUrls);
cb(data.QueueUrls);
}
});
}
// and in another file you would:
const {list_queues} = require('./list_queues.js');
list_queues(function(queues) {
console.log(queues);
});
I strongly recommend you to use promise based approach, since it's much more readable and you can make use of async/await on it, which is great.

Using Multiple FindOne in Mongodb

I am trying to extend the amount of fields that our API is returning. Right now the API is returning the student info by using find, as well as adding some information of the projects by getting the student info and using findOne to get the info about the project that the student is currently registered to.
I am trying to add some information about the course by using the same logic that I used to get the project information.
So I used the same findOne function that I was using for Projects and my logic is the following.
I created a variable where I can save the courseID and then I will put the contents of that variable in the temp object that sending in a json file.
If I comment out the what I added, the code works perfectly and it returns all the students that I require. However, when I make the additional findOne to get information about the course, it stops returning anything but "{}"
I am going to put a comment on the lines of code that I added, to make it easier to find.
Any sort of help will be highly appreciated!
User.find({
isEnrolled: true,
course: {
$ne: null
}
},
'email pantherID firstName lastName project course',
function(err, users) {
console.log("err, users", err, users);
if (err) {
return res.send(err);
} else if (users) {
var userPromises = [];
users.map(function(user) {
userPromises.push(new Promise(function(resolve, reject) {
///////// Added Code START///////
var courseID;
Course.findOne({
fullName: user.course
}, function(err, course) {
console.log("err, course", err, course);
if (err) {
reject('')
}
courseID = course ? course._id : null
//console.log(tempObj)
resolve(tempObj)
}),
///// ADDED CODE END //////
Project.findOne({
title: user.project
}, function(err, proj) {
console.log("err, proj", err, proj);
if (err) {
reject('')
}
//Course ID, Semester, Semester ID
//map to custom object for MJ
var tempObj = {
email: user.email,
id: user.pantherID,
firstName: user.firstName,
lastName: user.lastName,
middle: null,
valid: true,
projectTitle: user.project,
projectId: proj ? proj._id : null,
course: user.course,
courseId: courseID
}
//console.log(tempObj)
resolve(tempObj)
})
}))
})
//async wait and set
Promise.all(userPromises).then(function(results) {
res.json(results)
}).catch(function(err) {
res.send(err)
})
}
})
using promise could be bit tedious, try using async, this is how i would have done it.
// Make sure User, Course & Project models are required.
const async = require('async');
let getUsers = (cb) => {
Users.find({
isEnrolled: true,
course: {
$ne: null
}
}, 'email pantherID firstName lastName project course', (err, users) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
let findCourse = (users, cb) => {
async.each(users, (user, ecb) => {
Project.findOne({title: user.project})
.exec((err, project) => {
if (!err) {
users[users.indexOf(user)].projectId = project._id;
ecb();
} else {
ecb(err);
}
});
}, (err) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
let findProject = (users, cb) => {
async.each(users, (user, ecb) => {
Course.findOne({fullName: user.course})
.exec((err, course) => {
if (!err) {
users[users.indexOf(user)].courseId = course._id;
ecb();
} else {
ecb(err);
}
});
}, (err) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
// This part of the code belongs at the route scope
async.waterfall([
getUsers,
findCourse,
findProject
], (err, result) => {
if (!err) {
res.send(result);
} else {
return res.send(err);
}
});
Hope this gives better insight on how you could go about with multiple IO transactions on the same request.

Using async.js for deep populating sails.js

I have a big issue with my function in sails.js (v12). I'm trying to get all userDetail using async (v2.3) for deep populating my user info:
UserController.js:
userDetail: function (req, res) {
var currentUserID = authToken.getUserIDFromToken(req);
async.auto({
//Find the User
user: function (cb) {
User
.findOne({ id: req.params.id })
.populate('userFollowing')
.populate('userFollower')
.populate('trips', { sort: 'createdAt DESC' })
.exec(function (err, foundedUser) {
if (err) {
return res.negotiate(err);
}
if (!foundedUser) {
return res.badRequest();
}
// console.log('foundedUser :', foundedUser);
cb(null, foundedUser);
});
},
//Find me
me: function (cb) {
User
.findOne({ id: currentUserID })
.populate('myLikedTrips')
.populate('userFollowing')
.exec(function (err, user) {
var likedTripIDs = _.pluck(user.myLikedTrips, 'id');
var followingUserIDs = _.pluck(user.userFollowing, 'id');
cb(null, { likedTripIDs, followingUserIDs });
});
},
populatedTrip: ['user', function (results, cb) {
Trip.find({ id: _.pluck(results.user.trips, 'id') })
.populate('comments')
.populate('likes')
.exec(function (err, tripsResults) {
if (err) {
return res.negotiate(err);
}
if (!tripsResults) {
return res.badRequest();
}
cb(null, _.indexBy(tripsResults, 'id'));
});
}],
isLiked: ['populatedTrip', 'me', 'user', function (results, cb) {
var me = results.me;
async.map(results.user.trips, function (trip, callback) {
trip = results.populatedTrip[trip.id];
if (_.contains(me.likedTripIDs, trip.id)) {
trip.hasLiked = true;
} else {
trip.hasLiked = false;
}
callback(null, trip);
}, function (err, isLikedTrip) {
if (err) {
return res.negotiate(err);
}
cb(null, isLikedTrip);
});
}]
},
function finish(err, data) {
if (err) {
console.log('err = ', err);
return res.serverError(err);
}
var userFinal = data.user;
//userFinal.trips = data.isLiked;
userFinal.trips = "test";
return res.json(userFinal);
}
);
},
I tried almost everthing to get this fix but nothing is working...
I am able to get my array of trips(data.isLiked) but I couldn't get my userFInal trips.
I try to set string value on the userFinal.trips:
JSON response
{
"trips": [], // <-- my pb is here !!
"userFollower": [
{
"user": "5777fce1eeef472a1d69bafb",
"follower": "57e44a8997974abc646b29ca",
"id": "57efa5cf605b94666aca0f11"
}
],
"userFollowing": [
{
"user": "57e44a8997974abc646b29ca",
"follower": "5777fce1eeef472a1d69bafb",
"id": "5882099b9c0c9543706d74f6"
}
],
"email": "test2#test.com",
"userName": "dany",
"isPrivate": false,
"bio": "Hello",
"id": "5777fce1eeef472a1d69bafb"
}
Question
How should I do to get my array of trips (isLiked) paste to my user trips array?
Why my results is not what I'm expecting to have?
Thank you for your answers.
Use .toJSON() before overwriting any association in model.
Otherwise default toJSON implementation overrides any changes made to model associated data.
var userFinal = data.user.toJSON(); // Use of toJSON
userFinal.trips = data.isLiked;
return res.json(userFinal);
On another note, use JS .map or _.map in place of async.map as there is not asynchronous operation in inside function. Otherwise you may face RangeError: Maximum call stack size exceeded issue.
Also, it might be better to return any response from final callback only. (Remove res.negotiate, res.badRequest from async.auto's first argument). It allows to make response method terminal

Categories

Resources