Domanda

The idea is fairly simple we have a dust template creating check boxes and some static checkboxes.

  <div class="span10">
                <table class="table table-condensed">
                <caption class="pull-left">Favorite Cuisines:</caption>
                   <input type="hidden" id="myArray" />
                <tbody>
                    <tr>
                        {#data.cuisines}
                        <td>
                            <label class="checkbox inline">
         <input type="checkbox" name="cuisineFavorites" value="{_id}">{cuisineName}
                            </label>
                        </td>
                        {/data.cuisines}

                    </tr>
                    <tr>
                        <td>
                            <label class="checkbox inline">
                                <input type="checkbox" name="cuisineFavorites" value="name"> blank
                            </label>
                        </td>
                        <td>
                            <label class="checkbox inline">
                                <input type="checkbox" name="cuisineFavorites" value="american"> American
                            </label>
                        </td>
                        <td>
</div> 

snippet but the over idea is there, it uses the a hidden input myArray to store the selected checkboxes using jquery. In the actual case there is a lot of checkboxes.

{@eq key="userProfile" value=prtName}
<script type="text/javascript">
    var selectedCuisines = [];
    function addCuisine(cuisine) {
    if ($.inArray(cuisine, selectedFavorites) < 0) {
        selectedCuisines.push(cuisine);
        selectedCuisines.sort();
    }
    $("#myArray").val("[" + selectedCuisines.join() + "]");
    }
    function removeCuisine(cuisine) {
    var pos = $.inArray(cuisine, selectedCuisines);
    if (pos > 0) {
        selectedCuisines.splice(pos, 1);
        selectedCuisines.sort();
    }
    $("#myArray").val("[" + selectedCuisines.join() + "]");
    }

    $('input[name^=cuisine]').on("click", function(e) {
        if (this.checked === true) {
        addCuisine(this.value);
        } else {
        removeCuisine(this.value);
        }
    });
    </script>
{/eq}

This should be storing the selected checkboxes in myarray to send in post. So in the nodejs controller we output a few things, or at least try to and try to parse the value in myarray. most of the problem comes from the passing of undefined

exports.createProfile = function (req, res) {
console.log(JSON.stringify(req.body.myArray));
var profile = new Profile(req.body);
console.log("print something"); 
profile.cuisineFavorites = JSON.parse(req.body.myArray); 
profile._userId = req.user._id;
console.log("Saving profile:" + profile);
console.log("favorites" + JSON.stringify( profile.cuisineFavorites));
profile.save( function(err) {
if (err) {
    return res.render('users', {
    title: 'Your Profile',
    prtName: 'userProfile',
    error: utils.errors(err.errors),
    user: req.user
    });
}

return res.render('users', {
  title: 'Organization Profile',
  prtName: 'orgprofile',
  error: '',
  user: req.user
});
});

}

please help, don't know why its undefined, had the JSON.parse work a few times even when it still said undefined posting a sample output as well

SyntaxError: Unexpected token u at Object.parse (native) at exports.createProfile

it works here

  Model: {"title":"User Profile","prtName":"userProfile","user":{"_id":"532b781934
  fa1a7411000003","__v":0,"authToken":"","salt":"1387859137798","hashed_password":
  "e704f5c86a696a828fbb976446ee1243690b8917","provider":"local","username":"dw","g
  ivenName":"dw","familyName":"dw"},"data":{"cuisines":[{"_id":"532a4d281053cd042c
  000002","__v":0,"cuisineName":"American"},{"_id":"532a5c591053cd042c000003","__v
  ":0,"cuisineName":"Comfort Food"},{"_id":"532a5c5e1053cd042c000004","__v":0,"cui
  sineName":"Indian"},{"_id":"532a5c681053cd042c000005","__v":0,"cuisineName":"Med
  iterranean"},{"_id":"532a5c701053cd042c000006","__v":0,"cuisineName":"Portuguese
 "},{"_id":"532a5c791053cd042c000007","__v":0,"cuisineName":"Argentinian"}]}}

GET /users/profile/create 200 459ms

undefined print something Saving profile:{ _userId: 532b781934fa1a7411000003, _id: 532b781f34fa1a7411000004, cuisineFavorites: [ 532a4d281053cd042c000002, 532a5c591053cd042c000003, 532a5c5e1053cd042c000004 ], specialDiet: [], foodAllergies: '', officePhone: '', userTitle: '' } favorites["532a4d281053cd042c000002","532a5c591053cd042c000003","532a5c5e1053cd0 42c000004"]

but normally only prints

undefined print something, and then the above error is happening. I recently updated my install of klei-dust but that shouldn't effect the JSON

È stato utile?

Soluzione

don't bother using this type of method to send an array of data across anything. I remembered how post works. Name = cuisineFavorites in this case corresponds to every checkbox. Any checkbox that is selected when sent is post will be put into an array named cuisine favorites. In this case when we make a new profile and set req.body to it, the profile then has the cuisineFavorites set to any selected on the previous page

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top