as per the documentation the URL is optional whereas it is not
it is not working as well, the function is not waiting for the respone
var promise = teams.createMembership(
team_clientsID,
[],
'https://portal.xxxxxxxx.com',
user_id
);
promise.then(
function (response) {
return res.json({
Response: 'Success',
userId: user_id,
membership: response,
});
},
function (err) {
return res.json({
status: 'Error',
error: err.toString(),
});
}
);
return res.json({
status: 'EOF',
});
no errors no response from the promise
* @param {string} teamId
* @param {string[]} roles
* @param {string} url
* @param {string} email
* @param {string} userId
* @param {string} phone
* @param {string} name
* @throws {AppwriteException}
* @returns {Promise}
*/
createMembership(teamId: string, roles: string[], url: string, email?: string, userId?: string, phone?: string, name?: string): Promise<Models.Membership>;
What version SDK?
"dependencies": { "node-appwrite": "^9.0.0" }, "devDependencies": { "prettier": "^3.0.0" }
You're using an older SDK. That's why it's required.
Based on this code, the return is called before the promise is resolved. That's why you don't see anything
in fact i made ``` appwrite init function```` using appwrite-cli
You'll need to update dependencies if you want to use the newer SDK
i think it is a bug in the cli
anyways which sdk am i supposed to use
Look at the package on NPM
ok
11.0.0
the same nothig new on users.get() nor on createMemebership()
will catch up tomorrow, thanks a lot @Steven
please share your new code when you're back
Morning!
Well i miss reading of the documentation led to the above cocktail. Indeed the res.json({})
is received by the client and can not be reveiled in the fuction portal log.
as per the documentation
Response
If you need to send a response to the invoker of the function, such as a user, client app, or an integration, use the response object. The response information will not be logged to the Appwrite Console. There are several possible ways to send a response, explore them in the following Appwrite Function.
therefore the new code looks like that
import sdk from 'node-appwrite';
export default async ({ res, req, error, log }) => {
const client = new sdk.Client();
const teams = new sdk.Teams(client);
const users = new sdk.Users(client);
if (
!process.env.APPWRITE_FUNCTION_ENDPOINT ||
!process.env.APPWRITE_FUNCTION_API_KEY
) {
console.warn(
'Environment variables are not set. Function cannot use Appwrite SDK.'
);
} else {
client
.setEndpoint(process.env.APPWRITE_FUNCTION_ENDPOINT)
.setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(process.env.APPWRITE_FUNCTION_API_KEY);
// .setSelfSigned(true);
}
try {
const user_id = req.headers['x-appwrite-user-id'];
let user_email = '';
const team_clientsID = process.env.TEAM_ACCOUNTS_ID || '';
log(`Team ${team_clientsID} to join ${user_id}`);
log('Header ' + JSON.stringify(req.headers));
const userGetResp = await users.get(user_id);
user_email = userGetResp.email;
log('User ' + JSON.stringify(userGetResp));
var createMembershipResp = await teams.createMembership(
team_clientsID,
[],
undefined,
user_id
);
log('Create ' + JSON.stringify(createMembershipResp));
return res.json({
Response: 'Success',
// userId: user_id,
// email: user_email,
createMembershipResp: createMembershipResp.toString(),
});
} catch (err) {
error(err.toString());
return res.json({
status: 'Error',
error: err.toString(),
});
}
};
the Log is as follow
Team clients to join 64fbf57e2b39116d4d4e
Header {"host":"localhost","user-agent":"Appwrite/1.4.2","x-appwrite-trigger":"event","x-appwrite-event":"users.64fbf57e2b39116d4d4e.create","x-appwrite-user-id":"64fbf57e2b39116d4d4e","content-type":"application/x-www-form-urlencoded","connection":"keep-alive","content-length":"406"}
User {"$id":"64fbf57e2b39116d4d4e","$createdAt":"2023-09-09T04:33:02.177+00:00","$updatedAt":"2023-09-09T04:33:03.012+00:00","name":"","password":"$argon2id$v=19$m=65536,t=4,p=3$SWYzdkdvd1RXTnVoZUVzSQ$AkHd3UwyqwoRvQoC3iTD8e4FzQ1vQlTj6BBOg+O+PaM","hash":"argon2","hashOptions":{"type":"argon2","memoryCost":2048,"timeCost":4,"threads":3},"registration":"2023-09-09T04:33:02.177+00:00","status":true,"labels":[],"passwordUpdate":"2023-09-09T04:33:02.177+00:00","email":"qwerwr23elie@eswplus.com","phone":"","emailVerification":false,"phoneVerification":false,"prefs":{"avatar_id":null,"referred_by_agent_id":"","auto_funding":false,"first_name":"","last_name":"","address":"","country":""},"accessedAt":"2023-09-09T04:33:02.177+00:00"}
Create {"$id":"64fbf57f9b46335da809","$createdAt":"2023-09-09T04:33:03.636+00:00","$updatedAt":"2023-09-09T04:33:03.636+00:00","userId":"64fbf57e2b39116d4d4e","userName":"","userEmail":"qwerwr23elie@eswplus.com","teamId":"clients","teamName":"clients","invited":"2023-09-09T04:33:03.636+00:00","joined":"2023-09-09T04:33:03.636+00:00","confirm":true,"roles":[]}
as per the new code i was able to receive the client information and create the new membership without the need of the email with just the userId
Thanks a lot @Steven
[Solved]Appwrite 1.4.2 / function / Get User returns error:{}
Recommended threads
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Send Email Verification With REST
I am using REST to create a user on the server side after receiving form data from the client. After the account is successfully created i wanted to send the v...
- Use different email hosts for different ...
Hello, I have 2 projects and i want to be able to set up email templates in the projects. Both projects will have different email host configurations. I see ...