0

I have created array in controller and used that value in dropdown in angularjs view

controller :

$scope.menus = ['Home','WorkFlow','Statistics','Users','Projects','Setting']; 

view:

<select name="selectedRole" ng-model="selectedRole" ng-options ="x.role for x in roles">
     <option value="">Role</option>
</select>

In Server.js I wrote function to save

app.post('/addUser', function(req, res) {

 userCollection.save(req.body, (err, result) => {
    if (err) return console.log(err)

    console.log('Roles Added');
    res.redirect('/users')
    console.log('result')

  })

})

But I don't know how to save the selected Dropdown value in my mongodb. And am Using mongoclient

Kindly Any one help me to solve this Issue.

Vishnu
  • 745
  • 12
  • 32

2 Answers2

2

I would think of something like the following:

app.post('/addUser', function(req, res) {

 userCollection.save(req.body, (err, result) => {
    if (err) return console.log(err)

    const MongoClient = require('mongodb').MongoClient;

    MongoClient.connect(db.url, (err, database) => {
      if (err) return console.log(err)

      database.collection('users').insert({
        user: <blah-blah>,
        role: req.body.role
      }, (err, result) => {
          if (err) { 
            res.send({ 'error': 'An error has occurred' }); 
          } else {
            console.log('Roles Added');
            res.redirect('/users')
          }
        });
    })
  })

})

Please note that this code is far from production state, just an example. It also assumes that you're using mondodb npm package for connection. To install, run: npm install mongodb --save

Hope that helps.

Documentation

t1gor
  • 1,244
  • 12
  • 25
1

I fixed my issue .

   <select name="role" ng-model="role" >
                    <option ng-repeat="role_type in roles"  value="{{role_type._id}}">{{role_type.role}}</option>
                    </select>

I used ng-repeat instead of ng-option

Vishnu
  • 745
  • 12
  • 32