0

I am having a problem where I have a method where I have a data structure:

   const req = {
  ctx: {
    emails: [],
    newInvitationDto: {
      _type: 'NewInvitationDto',
      email: 'sethborne@gmail.com',
      groupId: '12345678',
      roleIds: ['organization.doctor'],
      firstName: 'Seth',
      lastName: 'Borne',
      title: 'Doctor',
      phone: '212.555.1212',
    },
  },
};

that comes from const ctx: Invitation = <Invitation>req.ctx, So when I console log ctx.emails I get an empty array.

Now when I try to push the value of email, that is 'sethborne@gmail.com' into the ctx.emails like so: ctx.emails.push(newInvitationDto.email) instead of getting a data structure like so:

['sethborne@gmail.com']

instead I get value of 1.

I believe something is wrong with my assignment, but not sure what it is.

Daniel
  • 14,004
  • 16
  • 96
  • 156
  • 1
    `.push` returns the new length of the array, maybe you are using that value accidentally? (it would be consistent with your observation). – Felix Kling Oct 12 '21 at 21:19
  • @FelixKling, ahh, I am trying to store the email inside of the `ctx.emails` array. – Daniel Oct 12 '21 at 21:20
  • Assuming that `newInvitationDto.email` is the value you claim it is, the code you have (`ctx.emails.push(newInvitationDto.email)`) will work. But the return value of `.push` is not an array. It will be `1` because the new length of the array is `1`. – Felix Kling Oct 12 '21 at 21:21
  • @FelixKling, how do I ensure the return value is an array? – Daniel Oct 12 '21 at 21:22
  • You cannot change the return value of built-in functions. You are not showing how/where you call `.push` so all I can say is that `return ctx.email;` would return the array from a function. – Felix Kling Oct 12 '21 at 21:24
  • @FelixKling, yes `ctx.emails` returns the empty array, and I want to store the email in there – Daniel Oct 12 '21 at 21:26
  • Based on the information you provided you do/have `const newInvitationDto = {email: 'sethborne@gmail.com'}; const ctx = {emails: []}; ctx.emails.push(newInvitationDto.email); console.log(ctx.emails);`, which works perfectly fine (as you can test for yourself in the console). If it doesn't work for you then please provide a [mcve]. – Felix Kling Oct 12 '21 at 21:30

0 Answers0