Is there a bug in updateUserFields? Unable to update language of (current) user

I’m failing in updating the language field of the current user in the backend using updateUserFields of wix-users-backend. Is there bug or am I doing something wrong?

This code is placed in the back-end (backend/crmModule.jsw)

import wixUsersBackend from 'wix-users-backend';
...
export async function updateContactDetails(contactId, contactInfo) {
let userResponse = await wixUsersBackend.updateUserFields(contactId, {
"firstName": contactInfo.firstName,
"lastName": contactInfo.lastName,
"language": contactInfo.language
})
let userDetails = await wixUsersBackend.getUser(contactId);
return userDetails;
}

This code is in the front end:

import { updateContactDetails } from 'backend/crmModule';
...
export async function dropdownLanguage_change(event) {
contact.firstName = $w("#inputFirstName").value;
contact.lastName = $w("#inputLastName").value;
contact.language = $w("#dropdownLanguage").value;
contact.country = $w("#dropdownCountry").value;
$w("#txtStatus").text = "" + JSON.stringify(contact);
let update = await updateContactDetails(contact.id, contact);
$w("#txtStatus").text = $w("#txtStatus").text + " >> after save >> " + JSON.stringify(update);
}

So the language of my user-account is “fr” but I want to change it to “nl”. After I changed it on the dropdown, it gives the following print on my page :

{
"id":"666c6cc0-cac2-4087-823f-7389f6f1023d",
"loginEmail":"tom.Xxxxx@somemail.com",
"slug":"tomXxxxx",
"language":"nl",
"firstName":"Tom",
"lastName":"Xxxxx",
"fullName":"Tom Xxxxx",
"picture":"https://someurl",
"email":"tom.Xxxxx@somemail.com",
"phone":"","country":"BEL"
} 
>> after save >> 
{
"id":"666c6cc0-cac2-4087-823f-7389f6f1023d",
"loginEmail":"tom.Xxxxx@somemail.com",
"memberName":"Tom Xxxxx",
"firstName":"Tom",
"lastName":"Xxxxx",
"nickname":"Tom Xxxxx",
"slug":"tomXxxxx",
"language":"fr",
"status":"ACTIVE",
"creationDate":"2019-02-16T19:54:59Z",
"lastUpdateDate":"2020-04-05T15:18:06.899Z",
"lastLoginDate":"2020-04-05T14:56:47Z",
"emails":["tom.Xxxxx@somemail.com"],
"phones":[],
"labels":["contacts-mobile","contacts-site_members_approved","contacts-new"],
"picture":{"url":"https://someurl"},
"groups":[],
"contactId":"666c6cc0-cac2-4087-823f-7389f6f1023d"
}

where you can see that the language didn’t update.
Worth mentioning, I did also attempts, changing the first and lastname too, and they did change, but not the language. So i’m pretty conviced my code is correct. Is this a bug of the UpdateUserFields?

Note in the API section…
Only the properties passed in the ContactInfo object will be updated. All other properties will remain the same.

So you might want to try removing contactInfo and putting in there what you want to be updated like as shown in the example.

export function myBackendFunction(firstName, lastName, email, phone) {

So from this.

export async function updateContactDetails(contactId, contactInfo) {

To this

export async function updateContactDetails(contactId, firstName, lastName, language) {

Thank you @givemeawhisky for your swift reply :slight_smile:
I agree on what the API states, but as I showed in my example I applied this logic on the updateUserFields. You are referring to updateContactDetails (a custom function) which is not the same as updateUserFields (as part of the wix-users-backend API).

let userResponse = await wixUsersBackend.updateUserFields(contactId, {
"firstName": contactInfo.firstName,
"lastName": contactInfo.lastName,
"language": contactInfo.language
})

So here I don’t pass an entire Object but only those fields who I want to be edited. The updateContactDetails is just a custom function that I wrote to easily group all kind of user info. This shouldn’t be an issue, or is it?
I even tried to hardcode the language, also negative:


let userResponse = await wixUsersBackend.updateUserFields(contactId, { 
"language": "nl" 
})

When I do this, this sucessfully alters first and last name:

let userResponse = await wixUsersBackend.updateUserFields(contactId, { 
"firstName": "Some", 
"lastName": "Name"
})

The updateContact from Wix CRM and the updateUserFields from Wix Users are the same method.

All you are doing differently is using the contactId with Wix CRM and the userId with Wix Users.

updateContact( )
Updates an existing contact.

Description
The updateContact() function returns a Promise that resolves when the contact with the specified ID has been updated.

Only the properties passed in the ContactInfo object will be updated. All other properties will remain the same.

Update an existing contact
This example contains a backend function that updates a contact.

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(contactId, firstName, lastName, email, phone) {
  wixCrm.updateContact(contactId, {
      "firstName": firstName,
      "lastName": lastName,
      "emails": [email],
      "phones": [phone]
  } )
    .then( () => {
      // contact has been updated
    } )
    .catch( (err) => {
      // there was an error updating the contact
    } );
}

updateUserFields( )
Updates an existing user by ID.

Description
The updateUserFields() function returns a Promise that resolves when the user with the specified ID has been updated.

Only the properties passed in the ContactInfo object will be updated. All other properties will remain the same.

Update a user

import wixUsersBackend from 'wix-users-backend';

export function updateUserFields(userId, firstName, lastName, email, phone) {
  wixUsersBackend.updateUserFields(userId, {
      "firstName": firstName,
      "lastName": lastName,
      "emails": [email],
      "phones": [phone]
  } )
    .then( () => {
      // contact has been updated
    } )
    .catch( (err) => {
      // there was an error updating the contact
    } );
}

The next question would be that if you are trying to update the users fields, then why are you using the contactId when you should be using userId?

@givemeawhisky , thank you for replying but the contactId contains the userId. This function is called from a page using the wixUsers.currentUser.id. So no worries. It contains the right ID. The proof of that is that in my reply to you I wrote that my function is actually updating, but not the language parameter. As you can look up, language is not available in Crm but only in User, that’s why I’m updating the userField. The firstName and LastName fields are indeed syncronised to the Crm as you mentioned.

In your example you don’t mention the language property, which is the only one I’m intrested in and this doesn’t work. All the other do work. So my question to you is, are you able to update the language property of the user by updateUserFields, I modified your example into this snippet. Does this work on your side?

export function updateUserFields(userId, firstName, lastName, language) {
  wixUsersBackend.updateUserFields(userId, {
      "firstName": firstName,
      "lastName": lastName,
      "language": language      
  } )
    .then( () => {
      // contact has been updated
    } )
    .catch( (err) => {
      // there was an error updating the contact
    } );
}

I tried to cut and paste it. But no luck. I did notice as well that the langage field between the member collection and the member info page in the dashboard are not synced! So my conclusion is: there is something not working there…

So I decided to implement a workaround: instead using standard language, I use my own custom field “spokenLanguage”, this updates too.

I tried to cut and paste it. But no luck. I did notice as well that the langage field between the member collection and the member info page in the dashboard are not synced! So my conclusion is: there is something not working there…
So I decided to implement a workaround: instead using standard language, I use my own custom field “spokenLanguage”, this updates too.

Hello! Can you tell us how you managed to do that? Async / Await ? .Then Promises ? Can you share the code please?

Backend:

export async function updateAddress(userId, phone, city, state, address) { 
    let userResponse  = await   wixUsersBackend.updateUserFields(userId, {
     "phones": [phone],
     "Address": address, // custom field
     "State": state, // custom field
     "City": city // custom field
     })
}

Frontend:

export async function fermOrder_click(event) {
    let phone = "555-3212" ;
    let city = "L.A.";
    let state = "California";
    let address = "Hollywood Test Address";
    let result = await updateAddress(userId, phone, city, state, address);		         
    try {
        if (result) {
    	    console.log(result);	
        } else {	
            console.log("no result");
        }
       } catch (err) {		
           console.log(err);	
       }
}

So the phone updates but the custom fields don’t . How did you do it?
Thank you!

The userId is specified in a global variable so it’s ok…

I’ve tried also the .then Promises but with the same outcome… The phone info is updating but the custom field don’t :(.

Well done for the workaround and just having a closer look at this, did you notice how the language is set.

https://www.wix.com/corvid/reference/wix-users-backend.html#UserInfo
Language - string
The member’s locale based on their last login.

https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields#language-language
Language (language)
Description: Displays the member’s locale based on their last login.
Type: Text
Can connect to data: Yes
Can use in dynamic page URL: Yes
Read-only: Yes

We can disregard the Wix CRM update contact method here as that just sets the language of the contact and there is nothing mentioned about it being linked with their usage etc.
https://www.wix.com/corvid/reference/wix-crm-backend.html#ContactInfo
Language - string
Contact’s language.

It would make sense that when the user is logged in you can let them change their language through the use of code that we have talked about in this post.

However, maybe it only works for when that user is actually online at that time, so if that user then logs off and logins back in later then their language is set back automatically to whatever their last locale was when they logged in before.

So in theory they would then be reverting back to the language settings that they were on before they went and changed them after their last login etc.

So this might be the reason why the users language is not being changed even though it has been changed in the code option supposedly.

Also, does it actually state their language in the Wix Dashboard through Customer Management for either Site Members or the Contact List? Mine just shows the address which gives you the country option.

If it is not shown anywhere and you can’t add it as an existing field through the Site Members add new field option.

Then when you are adding it as a custom field, are you not just changing the custom field and not actually changing the language in the Wix Members app collection etc?

Maybe we are thinking the wrong way here and this is the reason why you can not actually get the language field in the Dashboard.

We are both assuming that it should change and update the users language and it should stay that way regardless of anything, until the user wishes to change it again etc.

However, as it is always being set by the member’s locale based on their last login, then in theory it is not changeable completely, unless they change it to the same language that they are based in, which sort of makes the language change pointless if they change the language to another one which is different to where they are.

If a user changes it to say French and they are accessing the website in Italy, then after they log out and log themselves back in again later on, the language would have automatically be changed back from their chosen French and it would be back on Italian as that is their locale from the last login.

Yes you would think that the language choice would stay the same if the user specifically chooses that language, so if it does keep changing back based on their locale then some members will get annoyed with having to keep changing their language and it seeming to not be working each time they log back in.

@devtestshr , you have to add your customField first through the Contact List dashboard. Edit contact, add Field

Yes i’ve already done it… I can see them with getUser()… :(… But can’t update…

@devtestshr
You should not need to use a custom field for Address as it is already a field and it should already be shown in the contact info for that member.

You can see more about the Address field from Wix here.
https://www.wix.com/corvid/reference/wix-users-backend.html#Address

If you do not have Address in your contact info either through Contact List or Site Members, then just go to add field and add Address from the list, you do not need to go all the way to the bottom to add custom field option.

This will then add this section here.

Although please note that this is not listed in the ContactInfo for Wix CRM.

Nor is it in the UserInfo for the Wix Users

Therefore you might not actually be able to update it through the udateContact or updateUserFields method.

You can write data to these contact fields when you register/signup a user through a custom lightbox like here for example.

import wixUsers from 'wix-users';
import wixLocation from "wix-location";
import wixWindow from 'wix-window';

$w.onReady(function (){

$w('#submit').onClick(() => {

 let email = $w("#email").value;
 let password = $w("#password").value;
 let firstName = $w("#firstName").value;
 let lastName = $w("#lastName").value;
 let phone = $w("#phone").value;
 let street = $w("#street").value;
 let city = $w("#city").value;
 let zip = $w("#zip").value;
 let country = $w("#country").value;
 
 wixUsers.register(email, password, {
 contactInfo: {
 "firstName": $w('#firstName').value,
 "lastName": $w('#lastName').value,
 "phone": $w('#phone').value,
 "street": $w('#street').value,
 "city": $w('#city').value,
 "zip": $w('#zip').value,
 "country": $w('#country').value
}
}).
then(() => {
console.log('user registered successfully');
wixLocation.to('/plans-pricing');
}).catch((err) => {
console.log(`${err}: Error`)
});
});
});

Or this method.

import wixUsers from 'wix-users';
import wixLocation from "wix-location";
import wixWindow from 'wix-window';

$w.onReady(function (){

$w('#submit').onClick(() => {

 let email = $w("#email").value;
 let password = $w("#password").value;
 let firstName = $w("#firstName").value;
 let lastName = $w("#lastName").value;
 let phone = $w("#phone").value;
 let street = $w("#street").value;
 let city = $w("#city").value;
 let zip = $w("#zip").value;
 let country = $w("#country").value;
 
 wixUsers.register(email, password, {
 contactInfo: {
 "firstName": firstname,
 "lastName": lastName,
 "phone": phone,
 "street": street,
 "city": city,
 "zip": zip,
 "country": country
}
}).
then(() => {
console.log('user registered successfully');
wixLocation.to('/plans-pricing');
}).catch((err) => {
console.log(`${err}: Error`)
});
});
});

You might find that the only way to update the actual Address is to have the Wix Members app installed on your site and then let the actual user update it through their My Account page, which will then update the Address fields in the members contact info.

The My Addresses page is only available in the Wix Members app if you have added Wix Stores to your site as well…
https://support.wix.com/en/article/about-the-my-addresses-page-in-wix-stores

https://support.wix.com/en/article/about-the-my-addresses-page-in-wix-stores

@givemeawhisky Thank you!

This problem gave me headaches … That’s why i’ve tried Tom’s workaround.

I just can’t update the Address Info from any form outside “Members Pages-My account” page ( like using a Lightbox form ).

Also if you do Tom’s workaround with a custom field… it will work ! it worked only with Async/Await for me and probably for Tom too.

It was a bug from my end… i was using a function with the same name “updateAddress()” from another backend module (didn’t include the params) . :frowning: Sorry guys for trouble.