Search Posts

Category: mongoose

ngRepeat:dupes when trying to fetch from database

I am constantly getting the mentioned error no matter what I try. The error stops when I comment out the “fetchCourses()” in courses.js file. I am guessing the error is somewhere in the http request but I cannot figure it out. Here are my code snippets. courses.js (controller) var app = angular.module(“app”); app.controller(‘coursesCtrl’, [‘$scope’, ‘Courses’, ‘$routeParams’, function ($scope, Courses, $routeParams) { function fetchCourses() { Courses.getCourses({}).then(function (res) { $ =; }, function (err) { console.log(err); […]

How to get document by ID using mongoose with Angularjs

I am trying to get a single document by ID from a $http request using mongoose and Angularjs (learning Angular CLI). Any help would be greatly appreciated. I can get the id but cannot figure out how to pass it to the routing Controller for Single Article View: let _id = $routeParams; console.log(_id); $http({ cache: true, method: ‘GET’, url: ‘/article/:id’, params: { _id } }).then(function successCallback(res) { $scope.article =; }).catch(function errorCallback(err) { if (err) […]

AngularJS article view with MongoDB and Mongoose

I am trying to get an article to populate a modal when the user clicks ‘read more’. So I have a MongoDB setup using mongoose with an article schema and controllers for the view. Knowing that I need to use an ID of the article I cannot seem to figure this out. Check out the following code snips… Thank you for taking a look. ARTICLE VIEW– ARTICLES – great advice for the great outdoors! <h1 […]

update specific attribute in mongoose

I have a production MEAN web project and I need to modify specific attribute after being created‘/api/user’,function(req,res){ var user = req.body; console.log(req.body); userModel .create(user) .then( function(postObj){ var number =; res.json(200); },function(error){ // res.sendStatus(400); console.log(error) res.json(error) } ); }); Here I have to update specific attribute let’s assume it’s name gender and give it static value such as ‘male’ and I don’t want to have a callback or use the return object any help […]

Route.delete() requires a callback function but got a [object Undefined]

Keep getting this error but can’t figure out what is wrong with my delete function. “Route.delete() requires a callback function but got a [object Undefined]” const playerController = { index: (request, response) => { Player.find({}) .then(player => response.json(player)) .catch(error => console.log(error)); }, create: (request, response) => { Player.create(request.body) .then(player => response.json(player)) .catch(error => console.log(error)); }, delete: (request, response) => { Player.remove( .then(player => response.json(player)) .catch(error => console.log(error)); //console.log(‘node function’); } }; //- – – – […]

Mongoose query find() returns nothing

I’m querying db with 2 conditions, keyword and user id, both returns correctly in console log but returns nothing, not null, or [], just blank. Something is wrong with the syntax? app.get(“/api/client?”, function (req, res) { console.log(“Search > ” + JSON.stringify(req.query)); //Search > {“keyword”:”emily”,”filterby”:”5a25f3d1d4b3e30792dd53ca”} var keyword = req.query.keyword; var user_id =; Client.find({ $and: [{ “firstname”: new RegExp(‘^’ + keyword + ‘$’, “i”) }, { userid: user_id } ] }, (err, result) => { if […]

How to pass a parameter from angular 2 service to node js function? MEAN

I have a posts collection and each post has an id and userId. I want to find all posts for a specific user (using userId property).. here is my angular2 service: export class PostsService { private _url = ‘/api/posts’; constructor(private _http: Http) { } getPosts(): Observable<Post[]> { return this._http.get(this._url).map(res => res.json()); } getUserPosts(userId: number) { return this._http.get(this._url + ‘/’ + userId).map(posts => posts.json()); } } and my nodejs code : router.get(‘/posts’, function(req, res) { post.find({}).exec(function(err, […]

How can I include data from another collection in my mongoose object?

I am building an app with mongoDB and NodeJS with an Angular front end. I currently have users creating listings and then other users responding with bids within those listings, but want to include data of those responders. I am having a hard time including that user data in the bid create function and not sure why its not pulling. Here is the listing schema var mongoose = require(“mongoose”); var Schema = mongoose.Schema; var Bid […]

Instead of saving inside array, data is getting stored inside new record [duplicate]

This question already has an answer here: Push items into mongo array via mongoose 1 answer I am uploading images using MEAN stack and Multer module. I am able to retrieve images from the angular, and can even post image-paths to Mongoose collection. The problem is I am expecting an array of images but while posting to mongoose, it’s storing each image as a new record. Image schema var imageSchema=new Schema({ productId:{type: String,required: false}, imagePaths: […]

Accesing field outside of callback gives undefined in angular

I have a problem my data structure looks as follows: var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var schema = new Schema({ name: {type:String, required:true}, domain: {type:String, required:true}, kingdom: {type:String, required:true}, description: {type:String, required:true}, usages:[ {type : mongoose.Schema.ObjectId, ref : ‘Usage’} ], }); module.exports = mongoose.model(‘Flower’,schema,’flowers’); In database usages looks as follows: “usages”: [ { “$oid”: “59785b0c267c14036038324b” } ], after i populate page with data from http get: var app = angular.module(“flowerDataApp”, []); app.controller(‘dataPopulateController’, […]