Friday 19 May 2017

Get user ID to make AJAX post in NodeJS

I am trying to build a simple app that allows a user to signup, login, and search the weather of a particular city. The feature I am trying to add is to give the user the option to set their location so that every time they login, the current weather associated with their location is displayed.

Since this action is not done with a form but rather a button, I want to use AJAX to make a "post" request that will update that user's model and populate their "location" attribute with the city they have chosen. What I am struggling with is how to access the current user's ID in the Javascript file so that I can pass it into the AJAX request.

var setLocation = $('.location-input').val();
$('body').on('click', '.location-btn', function(){
    var data = {
      location: setLocation
    }
    $.ajax({
      url: '/users/' + userID,
      type: 'post',
      data: data,
      success: function(result){
        console.log(result)
      }
    })
  })

I would like to store the current user's ID in the variable "userId", but since I am not working within the EJS file, I am not quite sure how to access the ID from my standard javascript file. Can someone please help me here? Thank you so much!



via Ian Springer

No comments:

Post a Comment