Update Contact's picture problem

Page-Code:

export async function button1_click() {
  let user = await wixUsers.currentUser;
  let userID = await user.id;
  
  const updatedContactInfo={
     picture: {
       "image": "https://static.wixstatic.com/media/b03143_5705d86238124d7180578e64623112ba~mv2.png",
       "imageProvider": "EXTERNAL"
     }
  };

  overwriteContactInfo(userID, updatedContactInfo)
  .then(async(updatedContact) => {return updatedContact;})
  .catch((error)=>{console.error(error);});
}

Back-End:

export async function overwriteContactInfo(contactId, updatedContactInfo) {
 const myContact = await contacts.getContact(contactId);
 const contactIdentifiers = {
    contactId: contactId,
    revision: myContact.revision
 };
 const options = {
    allowDuplicates: false,
    suppressAuth: true
 };
 return await contacts.updateContact(contactIdentifiers, updatedContactInfo, options);
}

The code works fine for ADMIN to change PICTURE in Wix-CRM-Contacts, but do not work, with normal Accounts.

Am i missing something? SuppressAuth is also setted to —> TRUE.

Taking a look onto the VELo-API, it should be possible to UPDATE picture in wix-crm (contacts).


Why it is just working when—> logged in as —> ADMIN?

The ERROR what i get…

Any ideas how to solve this issue?

-it is possible to register with a pic/Photo, but the pic in the contacs do not automaticaly sync, so you have to update the pic in the contacts, after registration.

Reading the new wix-crm-backend-api it should be possible to Update and i also have already a working code, BUT–> it works only with an ADMIN-ACCOUNT. All other do not update the pic in the contacts after registration.

Is it possible to update a contact-image of any user?

Ok, i think i have to figure this out on my own, damn! :sweat_smile:

Hey there. Let me see if I can get someone to assist you! Thanks in advance for your patience…

@marlowe-shaeffer Thanks! :rose::rose::rose:

Just tried to work it out on my own and got strange code-behaviour…

When testing code on back-end, everything works just fine…

The CODE on BACKEND…

export async function updateContact() {
 let myContact = await contacts.getContact("c480c141-a87a-4816-8f34-d531bdd72462");
 let contactIdentifiers = {
     contactId: "c480c141-a87a-4816-8f34-d531bdd72462",
     revision: myContact.revision
 };

 const updatedContactInfo = {
    name: {
       first: "Annie",
       last: "New Name"
    },
 };
 
 const options = {
    allowDuplicates: true,
    suppressAuth: true
 };

 return contacts.updateContact(contactIdentifiers, updatedContactInfo, options)
 .then((updatedContact) => {return updatedContact;})
 .catch((error) => {console.error(error);});
}

Testing situation on backend…


Everything works just like a charm (on backend-testing).

  • REVISION-NUMBER accepted & working (checked) !
  • ID accepted & working (checked) !
  • IDENTIFIERS accepted & working (checked) !

But calling all this from FRONT-END …? what happens???

$w.onReady(()=>{console.log("Page-Ready")
    $w('#button2').onClick(()=>{console.log("Update-Contact running...")
        updateContact()
    });
});

I get all the way this error-message? What is different?

I am calling identical back-end-function without any changes.
The only changement is, that i am calling it from front-end.

Additional ERROR-LOG:

It is something like …

Hey there, Ninja :thinking:?

First of all, you don’t need to use async/await to to get the user ID as you’re accessing a property, no promise will be returned, it’s not even a function.

As a precautionary measure, try to pass the suppressAuth option to the getContact( ) as well, as the promise will be rejected if the user doesn’t have enough permissions.

const myContact = await contacts.getContact(contactId, { suppressAuth: true });

Also, on the last line, you should remove the “await” keyword as it’s not needed and might cause code issues, you’re returning the result, so the code will wait for the promise to be resolved or rejected anyway.

Lastly, try to implement a catch to figure out what and when errors occur.

try {
    // Your code here
} catch(err => {
    return Promise.reject({
        message: 'An error occurred when trying tp update the user',
        error: err // This is the real error, you can print it here or return it
    })
})

Doing this will tell you what went wrong during the excution of the code.

Hope this helps~!
Ahmad

Thanks Ahmad! I will try your tips…stay tuned :grin:

Removing → async/await causes ERROR…

@russian-dima I said remove the " await " from the last line from the backend code on your original code snippet where you have this:

return await contacts.updateContact(...)

Of course you need to await the promise to be resolved before you can access the revision of the contact.

@ahmadnasriya Ok, everything is FINE ! I got it!
Ahmad your tips are very useful (like always!) THANKS !

This was the error causing line…—>

let myContact =await contacts.getContact("c480c141-a87a-4816-8f34-d531bdd72462");

Your line here, works like a charm.

const myContact =await contacts.getContact(contactId,{ suppressAuth:true});

So it was a permission-issue.

Top-Answer!

@ahmadnasriya Yes i will optimize it … to be continued :grin:

@russian-dima always happy to help :blush:

Hey @russian-dima

I see it’s fail on the backend file. You need provide permissions for contacts.getContact method too.

const myContact = await contacts . getContact ( contactId , {
suppressAuth : true ,
});

Thanks, but already solved. Yes it was a permission-issue.
I did not find any hint on this in the VELO-API.

Perhaps this could be added, to prevent further user-confusions.

Thanks anyway for your attention.

Спасибо!:wink:

@ahmadnasriya Haha, yes now i understand your suprising reaction, related to —> “Ninja” :joy::joy::joy:

What does it tell us?
Do not leave VELO-FORUM for to long period of time, because things changes quickly :stuck_out_tongue_winking_eye:

But you know who i am :rofl:

@russian-dima I know who you are, my surprising was due to the actual nickname ‘Ninja’, I tried to imagine, why Ninja? Does he walk on the wall or something?

Yes I was away for a long time, but I was always monitoring the forum, even if I didn’t leave a trace.

@ahmadnasriya :rofl: i just wanted to memorize my old state → Ninja​:sweat_smile:
And so → “Velo-Ninja” was born.:rofl:

-HE fights against the dark unknown APIs.
-He tries to break trough boundaries.
-He is always here watching you

—> Velo-Ninja :rofl::stuck_out_tongue_winking_eye:

See you next time my friend, hopefully more frequent again :v:

@russian-dima Ah, right, I was a Ninja before, now you name will remember me of our previous badge :joy:

Yeah, yeah, we’re brave, and … we can walk on walls too :wink: