0

So im making this inventory website, so the forklift drivers can add or remove weight from our inventory we measure our products in weight.

So basically I need to be able to fetch the weight (in kg) from mongo db and add it to it and save it

//edit the problem I'm having is the current code is returning "NaN kg" in html

kg is defined by a number inserted into the db and i cant seem to get the kg value without my code running into a error

This is the web part This is the modal adding the kg value

The Html      

     <form class="add-pro">
      <input type="text" class="form-control" name="namn" required="true" placeholder="Produkt Name"/>
      <br />
      <input type="text" class="form-control" name="id" required="true" placeholder="Produkt code"/>
      <br />
      <input type="number" class="form-control" placeholder="KG" name="kg" />
      <input id="btnModal" type="submit" value="add" class="btn btn-primary"/>
    </form>



      <form class="add-data">
        <input type="number" class="form-control" id="add-data-control" name="g" placeholder="how much">
        <button class="glyphicon glyphicon-plus" type="submit" id="add-data-plus" aria-hidden="true"></button>
      </form>

Javascript

"submit .add-data": function(event){
  var g = event.target.g.value;
  var x = produkter.find().fetch();
  var k = kg;
  produkter.update(this._id, {$set: {kg: +k + +g }});
},


    Template.produkter.events({
"submit .add-pro": function(event){
var namn = event.target.namn.value;
var id = event.target.id.value;
var kg = event.target.kg.value;

produkter.insert({
  namn: namn,
  id: id,
  kg: kg
});
  return false;

},

2
  • What is your problem exactly? Commented Jan 27, 2017 at 14:10
  • @Khang edited it to make it more clear i can give more info if needed Commented Jan 27, 2017 at 16:00

1 Answer 1

1

There are a few problems with your code, see comments:

"submit .add-data": function(event){
  var g = event.target.g.value;
  var x = produkter.find().fetch();
// Where is kg defined?
  var k = kg;
// The expression +k + +g does not compute - do you mean k+g ?
  produkter.update(this._id, {$set: {kg: +k + +g }});
},

You may also be running into trouble with strings versus numbers. Even though your HTML input tag says type="number", the value will be a string, and will need to be converted to a number before saving it to the database.

I suspect you are also intending to save the value as something like "2.7 kg", which is useful for displaying the weight, but it's a bad idea, because if you do that you will need to strip off the " kg" every time you want to calculate a new value.

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

2 Comments

Kg is defeind in a input feild for adding a produkt to a list then i run the collection.insert({kg: kg}); and i get the value from an input <input name="kg"> i also have the kg after the value printed inside of a "p" tag @Mikkel
Your code is not complete - I can't tell what's wrong with it... better edit the code snippets to include the relevant fields. One thing is also certain, this expression will cause an exception (probably the cause of the NaN (Not a Number))

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.