Is there an easy way to create a custom profile when creating a new user?

So maybe I’ve missed something, and I hope I have, because I think my logic can be simplified. I’m using Typescript, but if you have a solution I can probably figure it out.
Basically, I want to create a Profile object that will store a user’s name, profile photo, etc. I don’t want to store this in the system’s User table because every time I send the object up, it expects a Backendless.User object, not any custom class that I create.
If there’s an easy way to send up something other than Backendless.User, that’s probably the simplest solution.
The other option is:

let user = null



UserService.register(backendlessUser)
    .then(createdUser => {
 user = createdUser
 let profile = new Profile({ownerId: createdUser.objectId})
 return Persistence.of(Profile).save(profile)
 })
 .then(createdProfile => {
 user.profile = createdProfile
 return UserService.update(user)
 })
 .then(updatedUser => {
 // only here is both the user and profile created, and both linked to each other
 })
 .catch(err => { /* whatever */ })

Is there a way to accomplish this without making 3 separate calls to the server?

How about this:

backendlessUser.profile = new Profile({ put any profile props here});
UserService.register(backendlessUser)
.then(registeredUser => {
 // do whatever with the user

 })
 .catch(err => { /* whatever */ })

That works perfectly. I had tried that earlier but kept getting the “missing __class property” because I thought there were only 2x _ before the class definition and not 3x, so I thought it was buggy.

Is there a benefit to having both a Profile and User class? As of now I do it because of class definitions in Typescript. If there’s an easy way to use dot notation while modifying the User object, I’m all ears.

let user = new Backendless.User()
user.profile = new Profile() // this will cause a pre-compile error in TS
user['profile'] = new Profile() // this will work, but sucks with autocomplete, refactoring, etc

Do you know of a way to be able to use the user.profile?

Hi Matt

I don’t think that separate user object on two classes (User, Profile) is good idea. Each user can not has more than one Profile, right? you can add any props/relations to the User object. In my opinion: “less code - less problems” =)

But if you need this we can try to do that

As I understand you are using TypeScript and you need to describe User class by your own prop types, right?

For the first try to declare a primitive properties for User class

 class User extends Backendless.User {
    fullName: string;
    age: number;
    superUser:boolean
 }

 var user: User = new User();
 user.email = 'daxter@morgan.com';
 user.password = "my_super_password";
 user.fullName = "Dexter Morgan";
 user.age = 35;
 user.superUser = false;
 const currentUser:User = Backendless
    .UserService
    .login<User>(user.email, user.password, true)

Now declare Profile class and add to User class


 class UserProfile {
    ___class: string = 'Profile';

    firstName: string;
    lastName: string;

    constructor(profile: Object) {
        Object.assign(this, profile)
    }

    getFullName() {
        return this.firstName + ' ' + this.lastName
    }
 }

 class User extends Backendless.User {
    profile: UserProfile;
 }

 var user: User = new User();

 user.profile = new UserProfile({firstName: 'Dexter', lastName: 'Morgan'});
 user.profile.firstName = 'Jim';

Hope it will helped you

Regards, Vlad

My original goal was to use the class User extends Backendless.User but that results in a TS error unfortunately (stuck in TS because I’m using Ionic 2).

Error: Type ‘any’ is not a constructor function type (referring to Backendless.User)

could you provide part of your TS code where the TS compiler fails?

http://support.backendless.com/public/attachments/a042640575f2adaef4c92e23697d6dd5.png</img>

or
http://support.backendless.com/public/attachments/999c4d19ed8440279ddbbd5a79a4a1ea.png</img>

Both result in the error: “TS2507: Type ‘any’ is not a constructor function type.”

999c4d19ed8440279ddbbd5a79a4a1ea.png

a042640575f2adaef4c92e23697d6dd5.png

looks like you declared Backendless module as “any” manually

declare var Backendless:any;

instead that you need to add reference on backendless.d.ts once,
In my projects I have tsd.d.ts file in root and add all typescript-definition references what I need

/// &lt;reference path=&quot;node_modules/backendless/libs/backendless.d.ts&quot; /&gt;

and then you can use



import Backendless from 'backendless' 

It should help

Ahh, so used to using @types/ that I forgot Backendless doesn’t have one. Gotta use the old school reference path. Thanks.

So I’m running into an issue after using /// <reference path>. Promises no longer work.

Backendless.Data.of(User).findById(userObjectId)
.then(userData => {
console.log(userData)
})
.catch(err => {console.log(err)})

Will give me an error on .then() because "Property ‘then’ does not exist on type ‘Object’. When I follow findById to backendless.d.ts, there are 3 functions, the first of which returns an Object. The last returns a Promise, but for some reason my TS transpiler cannot find that function. It works without the /// <reference path>.

Hey Matt

Try this one:

Backendless.Data.of(User)
  .findById&lt;Promise&gt;(userObjectId)
  .then(userData => {
     console.log(userData)
  })
  .catch(err => {console.log(err)})

That gives me the error “Generic Type ‘Promise<T>’ requires 1 type argument(s).”

I was able to get it work via

Backendless.Data.of(User).findById&lt;Promise&lt;User&gt;>(userObjectId)
.then(doSomething)
.catch(catchErr)

Little convoluted, but if it works…

Yes, You are right, I forgot about this