0

This is my jade file (basically # stand for id and . for class) and I want to send this to mongodb and save user.activity

textarea#post-form.form-control(
    rows="4",
    placeholder="Share to world of LinkMe",
    ng-model='user.activity'
)

button#post-btn.btn.btn-primary.pull-right(ng-click="post(user)") Link

Here is my code for the controller:

$scope.post = function(user){
    auth.post(user).then(function(){
        $scope.activity = user.activity;
    })
}

And the auth.post function :

post: function(user){
    var deferred = $q.defer();

    var updatedUser = new UsersResource(user);
    updatedUser._id = identity.currentUser._id;

    updatedUser.$update().then(
        function(){
            identity.currentUser.activity = updatedUser.activity;
            deferred.resolve();
        }, 
        function(response){
            deferred.reject(response);
        }
    );

    return deferred.promise;
}

Good thing is I see the change when this happens, but it is not saved in the database.

This is the userSchema:

var userSchema = mongoose.Schema({
    username: {type: String, require: '{PATH} is required' , unique: true},
    firstName: {type: String, require: '{PATH} is required'},
    lastName: {type: String, require: '{PATH} is required'},
    profilePic: {type: String, default: 'imgs/default.jpg'},
    activity: String,
    salt: String,
    hashPass: String,
    roles: [String]
});

I really appreciate help :)

P.P. I have this on server now :

app.put('/collections/:collectionName/:id', function(req, res, next) {
  req.collection.updateById(req.params.id, {$set:req.body}, {safe:true, multi:false}, function(e, result){
    if (e) return next(e)
    res.send((result===1)?{msg:'success'}:{msg:'error'})
  })
})

Problem now is that I have model of User from userSchema but I didn't declare any collections. What should I do ?

2
  • There is no server side code posted here. So we do not know what is happening on that side. You need to post some code for that in your question. Commented Jun 22, 2014 at 10:31
  • Well , here is my userSchema (I updated my post up there). Apart from that, I have nothing else . If I need more things what should I have on the server then? Commented Jun 22, 2014 at 13:05

1 Answer 1

1

You'll need a REST interface on the server side to work with Mongo, take a look at this:

http://docs.mongodb.org/ecosystem/tools/http-interfaces/#HttpInterface-RESTInterfaces

Let's say you are using Node.js with Express and Mongoose, you will have to set up a REST api along the lines of:

var app = express();
var mongoose = require('mongoose');
var database_url = "mongodb://localhost:27017/Database";
var listen_port = 8080; // example port
var collections = ["collection1",...];

mongoose.connect(database_url);

var Model1 = mongoose.model('collection1', userSchema); // <--- your user schema

// example of a getter by id 

app.get('/api/get/:id', function(req, res) {
Model1.find({ _id: req.params.id }, function(err, post){
    res.send(post);
});
});

app.listen(listen_port);

and set up dependencies in your package.json file, like for instance

"dependencies" : {
    "express"    : "~3.4.4",
    "mongoose"   : "~3.6.2"
}

And then make use of Angular $http service in order to perform async calls to the API.

https://docs.angularjs.org/api/ng/service/$http

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you really much for this answer :) Upvoted and added as best answer :) I think that's the least I can do for you.
no problem, if you encounter any other problems I can try to help
I need a little help about collections as I am not sure what they are. I have a model and am not sure how to declare collection and use it. I edited my post up so you can see what is in my code , if you have enough time you can add me in skype svetla_venci1 . I really need that help fast because I have a dead-line until 25.06
Sorry I am on a deadline too :D. Have you tried this? : docs.mongodb.org/manual/tutorial/getting-started It's pretty straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.