Hi everybody, I’m still new to Meteor (and React for that matter) but so far it has been great. I’m trying to update the price of a single item in a collection but something is off because the item I’m trying to update always gets updated to the last item added to the collection.
Below is how I add an item to the collection…
addItem = e => {
e.preventDefault();
if (this.newItemName.value !== ‘’) {
PantryDB.insert({
name: this.newItemName.value.trim(),
price: this.newItemPrice.value.trim(),
stocked: false
});
}
this.newItemName.value = '';
this.newItemPrice.value = '';
};
…and the form that takes in the info…
<form className="Pantry__form" onSubmit={this.addItem}>
<div className="Pantry__form-content">
<label htmlFor="newItemName">Name: </label>
<input type="text" ref={input => (this.newItemName = input)} />
<label htmlFor="newItemPrice" id="marginLabel">
Price:{' '}
</label>
<input type="text" ref={input => (this.newItemPrice = input)} />
</div>
<button type="submit">Add Item</button>
</form>
This all works great and I then map over the DB and return each item in it, each item then has an input element that displays it’s price and calls the update method onChange…
<input
className=“Item__price”
type=“text”
defaultValue={item.price}
id={item._id}
ref={input => (this.price = input)}
onChange={this.updatePrice}
/>
…and the update method that gets called…
updatePrice = e => {
e.preventDefault();
const _id = e.target.id;
const itemToUpdate = PantryDB.find({ _id: e.target.id }).fetch();
PantryDB.update(itemToUpdate[0]._id, { $set: { price: this.price.value.trim() } });
};
Any thoughts? I’m sure I’m not binding the item correctly perhaps but I can’t seem to figure it out! Any help would be greatly appreciated!
Thanks