Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Byte your tongue.


devel / comp.lang.javascript / Uncaught TypeError: data.find is not a function

SubjectAuthor
* Uncaught TypeError: data.find is not a functionKiran Chavan
`- Re: Uncaught TypeError: data.find is not a functionJon Ribbens

1
Uncaught TypeError: data.find is not a function

<270341dc-c840-4cec-99f7-94a3166bcd58n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=5337&group=comp.lang.javascript#5337

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:ad4:5ba7:0:b0:473:6526:f6e6 with SMTP id 7-20020ad45ba7000000b004736526f6e6mr578380qvq.90.1657430570226;
Sat, 09 Jul 2022 22:22:50 -0700 (PDT)
X-Received: by 2002:a05:6808:20a6:b0:335:b651:80ea with SMTP id
s38-20020a05680820a600b00335b65180eamr4195837oiw.57.1657430569871; Sat, 09
Jul 2022 22:22:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Sat, 9 Jul 2022 22:22:49 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2405:201:d01a:f0d2:ddb4:79b1:11c9:c351;
posting-account=bmVj5goAAAAPjzgVipOfK1vZnRwv6mdf
NNTP-Posting-Host: 2405:201:d01a:f0d2:ddb4:79b1:11c9:c351
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <270341dc-c840-4cec-99f7-94a3166bcd58n@googlegroups.com>
Subject: Uncaught TypeError: data.find is not a function
From: kiranishere45@gmail.com (Kiran Chavan)
Injection-Date: Sun, 10 Jul 2022 05:22:50 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 67
 by: Kiran Chavan - Sun, 10 Jul 2022 05:22 UTC

I am trying to convert the model instances into a JSON object and then according to the value selected in the dropdown menu, I want to assign its amount to the `line_one_unit_price` text field in the form.

Javascript code:

{{ data|json_script:"hello-data" }}
<script type="text/javascript">
const data = JSON.parse(document.getElementById('hello-data').textContent);

document.getElementById('id_line_one').onchange = function(event){
let elementInData = data.find(() => item.pk == event.target..value);
document.getElementById('id_line_one_unit_price').value = elementInData && elementInData.amount ? elementInData.amount : 0;
};

</script>

I guess, I am getting the error at `data.find` because `.find` is used on an array whereas the constant `data` is not an array? How do I convert that JSON object into an array? how do I assign the amount of the `title` selected in the forms to the `line_one_unit_price`?

[![ss][1]][1]

When I click some title from the dropdown, I want the amount to be displayed in the `Unit Price`.

`views.py`:

def add_invoice(request):
form = InvoiceForm(request.POST or None)
data = serializers.serialize("json", Inventory.objects.all())
total_invoices = Invoice.objects.count()
queryset = Invoice.objects.order_by('-invoice_date')[:6]

if form.is_valid():
form.save()
messages.success(request, 'Successfully Saved')
return redirect('/invoice/list_invoice')
context = {
"form": form,
"title": "New Invoice",
"total_invoices": total_invoices,
"queryset": queryset,
"data": data,
}
return render(request, "entry.html", context)

`models.py`:

class Inventory(models.Model):
product_number = models.IntegerField(primary_key=True)
product = models.TextField(max_length=3000, default='', blank=True, null=True)
title = models.CharField('Title', max_length=120, default='', blank=True, unique=True)
amount = models.IntegerField('Unit Price', default=0, blank=True, null=True)

def __str__(self):
return self.title

Thanks.

[1]: https://i.stack.imgur.com/9x6uC.jpg

Re: Uncaught TypeError: data.find is not a function

<slrntcln4k.r4j.jon+usenet@raven.unequivocal.eu>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=5338&group=comp.lang.javascript#5338

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.javascript
Subject: Re: Uncaught TypeError: data.find is not a function
Date: Sun, 10 Jul 2022 14:05:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <slrntcln4k.r4j.jon+usenet@raven.unequivocal.eu>
References: <270341dc-c840-4cec-99f7-94a3166bcd58n@googlegroups.com>
Injection-Date: Sun, 10 Jul 2022 14:05:08 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="925ad098814b4bda65c0d0c005799037";
logging-data="1476835"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zVojcr8pBEdpQiEfDXcX1LGuJxR5K2Xg="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:h8MQkThto+VuQX66AH2zZRZXj9o=
 by: Jon Ribbens - Sun, 10 Jul 2022 14:05 UTC

On 2022-07-10, Kiran Chavan <kiranishere45@gmail.com> wrote:
> I am trying to convert the model instances into a JSON object and then
> according to the value selected in the dropdown menu, I want to assign
> its amount to the `line_one_unit_price` text field in the form.
>
> Javascript code:
>
> {{ data|json_script:"hello-data" }}
....
> I guess, I am getting the error at `data.find` because `.find` is used
> on an array whereas the constant `data` is not an array? How do I
> convert that JSON object into an array?
....
> def add_invoice(request):
> form = InvoiceForm(request.POST or None)
> data = serializers.serialize("json", Inventory.objects.all())

The key problem is that you are *double*-encoding your data as JSON.
First using 'serializers.serialize', and then again with 'json_script'.

You'd have to send it through JSON.parse twice in order to turn it
back into the objects you want - but of course that would be the wrong
fix, the correct fix would be to only JSON-encode it once. But note,
removing the 'json_script' filter would not be the right way to do that.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor