I am writing a Pantry Inventory app for a school project with a Django backend, a DjangoRest API and a Vue2 frontend.
I am trying to discern if an item is within one week of the expiration date tied to each of the items, and change expiring_soon to true if it is.
My model in Django takes the expiration date through a DateTimeField that is serialized as such:
class PantrySerializer(serializers.ModelSerializer): owner_detail = NestedUserSerializer(read_only=True, source='owner') expiration_date = serializers.DateTimeField(format="%d-%m-%Y") class Meta: model = models.PantryItem fields = ( 'id', 'item_name', 'create_date', 'expiration_date', 'owner', 'owner_detail', 'getting_low', 'tag', 'category', 'quantity', 'to_grocery', 'slug', 'amount_type', 'expiring_soon', 'brand', )
After browsing some related questions I've tried this as a Vue method in my root app. I call it in the created section.:
getExpiry: function() { var one_day=1000*60*60*24; var currentTime = new Date() for (let i = 0; i < this.pantryItems.length; i++) var exdate = this.pantryItems[i].expiration_date; var diff = Math.ceil((exdate.getTime() - currentTime.getTime())/one_day); if (diff <= 7) { this.pantryItems[i].expiring_soon = true updateItem() } }, gettingLow: function(item) { item.getting_low = true }
It gives me this error:
VM22198 vue.js:634 [Vue warn]: Error in created hook: "TypeError: Cannot read property 'getTime' of undefined" (found in <Root>) warn @ VM22198 vue.js:634 logError @ VM22198 vue.js:1893 globalHandleError @ VM22198 vue.js:1888 handleError @ VM22198 vue.js:1848 invokeWithErrorHandling @ VM22198 vue.js:1871 callHook @ VM22198 vue.js:4220 Vue._init @ VM22198 vue.js:5002 Vue @ VM22198 vue.js:5078 (anonymous) @ (index):289 VM22198 vue.js:1897 TypeError: Cannot read property 'getTime' of undefined at Vue.getExpiry ((index):519) at Vue.created ((index):537) at invokeWithErrorHandling (VM22198 vue.js:1863) at callHook (VM22198 vue.js:4220) at Vue._init (VM22198 vue.js:5002) at new Vue (VM22198 vue.js:5078) at (index):289
Any help is greatly appreciated!
https://stackoverflow.com/questions/65784591/trying-to-compare-current-time-to-expiration-date-and-return-true-if-within-7-da January 19, 2021 at 10:07AM
没有评论:
发表评论