Ember component function outside of action function

pusle

I have a problem calling a function outside action functions from an actions function. As you can see from the code below, I have a selectClient action that calls two functions, createCompanyAccount and createPrivateAccount. But I always get a this.createPrivateAccount is undefined. I have tried using self, but to no avail. Weirdly, I thought I would have to use self.createCompanyAccount, but then I get a self.createCompanyAccount is not defined.

I use Ember 2.12 and Ember Data 2.16.3.

import Ember from 'ember';

export default Ember.Component.extend({
    store: Ember.inject.service(),
    tagName: '',

    /**
     * Actions
     */
    actions: {
        // Select from selectList
        selectClient(element) {
            let self = this;

            if (element.company) {
                this.get('store').query('account', { 'filter' : {'orgnumber': element.orgNumber}}).then(
                    (accounts) => {
                        /* Organisation exist already */
                    },
                    (error) => {
                        let code = Number(error.errors[0].status);
                        if (code === 404) {
                            // company does not exist, so lets create it, and an account.
                            this.createCompanyAccount(element).then(
                                (account) => {
                                    /* Do stuff... */
                                }
                            );
                        }
                    }
                );
            } else {

                this.createPrivateAccount(element).then(
                    (anonUser) => {
                        /* Do stuff... */
                    }
                );

            }
        }
    },

    createCompanyAccount(company) {
        let account = this.get('store').createRecord('account', {
            type: 'company',
        });

        // Check if postal address is set on result
        if (typeof company.addressObject !== 'undefined') {
            let postAddress = this.get('store').createRecord('address', {
                address: company.addressObject.streetName,
                zip: company.addressObject.zip,
                postal_address: company.addressObject.postalAddress
            });
            account.get('addresses').pushObject(postAddress);
        }

        this.get('store').createRecord('company', {
            name: company.name,
            org_number: Number(company.orgNumber),
            account: account
        }).save().then((new_company) => {
            return new_company.get('account');
        });

    },

    createPrivateAccount(person) {
        let account = this.get('store').createRecord('account', {
            type: 'anonuser'
        });

        // Check if postal address is set on result
        if (typeof person.addressObject !== 'undefined') {
            let postAddress = this.get('store').createRecord('address', {
                address: person.addressObject.streetName,
                zip: person.addressObject.zip,
                postal_address: person.addressObject.postalAddress
            });
            account.get('addresses').pushObject(postAddress);
        }

        this.get('store').createRecord('anonUser', {
            first_name: person.firstName,
            sur_name: person.surName,
            email: person.email,
            phone: person.phone,
            account: account,
        }).save().then((new_person) => {
            return new_person.get('account');
        });
    }

});

Can anyone see where I go wrong? I can note that there is a few other functions that I have removed for clarity.

Thank you, Tommy

jelhan

Your issue is not about this.createPrivateAccount and this.createCompanyAccount being undefined. I think both of them are being executed but they do return undefined but you are expecting a Promise. Therefore this.createPrivateAccount().then is undefined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting the name of a function provided to an Ember `action` helper

From Dev

Variable not available outside of a function in a react component

From Dev

Ember Component Action Not Bubbling

From Dev

redux action "is not a function" when dispatched from component

From Dev

How can I bubble up an Ember action inside a callback function?

From Dev

Call action function when template is loaded (Ember.js)

From Dev

Ember nested component action not bubbling

From Java

Angular2 - how to call component function from outside the app

From Dev

How to render component from a function outside the render method in React?

From Dev

Rendering ember component outside parent template

From Dev

Ember, mixin to detect click outside of view/component

From Dev

How to ensure a child component calls parent function after Ember.run.later is completed in Ember

From Dev

Function works inside "$(function() {" but not outside?

From Dev

call var in function to outside the function

From Dev

Ember: Bubble action from component to application controller

From Dev

Calling controller action from Ember 2.0 component

From Dev

Ember: send action from one component to another

From Dev

Access jquery event from ember component action

From Dev

Ember: send action from one component to another

From Dev

Access jquery event from ember component action

From Dev

Ember 2 action sent to route instead of component

From Dev

Ember: globally-available search component (and action)?

From Dev

Ember : addObject/ pushObject is not a function

From Dev

using variable outside of a function

From Dev

Return Object Outside of Function

From Dev

Use a variable outside function

From Dev

Call a function outside main ()

From Dev

Use $(this) in an outside function

From Dev

Return outside function error

Related Related

  1. 1

    Getting the name of a function provided to an Ember `action` helper

  2. 2

    Variable not available outside of a function in a react component

  3. 3

    Ember Component Action Not Bubbling

  4. 4

    redux action "is not a function" when dispatched from component

  5. 5

    How can I bubble up an Ember action inside a callback function?

  6. 6

    Call action function when template is loaded (Ember.js)

  7. 7

    Ember nested component action not bubbling

  8. 8

    Angular2 - how to call component function from outside the app

  9. 9

    How to render component from a function outside the render method in React?

  10. 10

    Rendering ember component outside parent template

  11. 11

    Ember, mixin to detect click outside of view/component

  12. 12

    How to ensure a child component calls parent function after Ember.run.later is completed in Ember

  13. 13

    Function works inside "$(function() {" but not outside?

  14. 14

    call var in function to outside the function

  15. 15

    Ember: Bubble action from component to application controller

  16. 16

    Calling controller action from Ember 2.0 component

  17. 17

    Ember: send action from one component to another

  18. 18

    Access jquery event from ember component action

  19. 19

    Ember: send action from one component to another

  20. 20

    Access jquery event from ember component action

  21. 21

    Ember 2 action sent to route instead of component

  22. 22

    Ember: globally-available search component (and action)?

  23. 23

    Ember : addObject/ pushObject is not a function

  24. 24

    using variable outside of a function

  25. 25

    Return Object Outside of Function

  26. 26

    Use a variable outside function

  27. 27

    Call a function outside main ()

  28. 28

    Use $(this) in an outside function

  29. 29

    Return outside function error

HotTag

Archive