Hello appwriter & team i'm trying to subscribe my already existing users to a topic but its not working, it just get stuck at a place
TypeScript
// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
const client = new Client();
client.setEndpoint(process.env.MY_APPWRITE_ENDPOINT).setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID).setKey(process.env.APPWRITE_API_KEY);
const functions = new Functions(client);
const database = new Databases(client);
const storage = new Storage(client);
const users = new Users(client);
const ENDPOINT = process.env.MY_APPWRITE_ENDPOINT;
const PROJECT_ID = process.env.APPWRITE_FUNCTION_PROJECT_ID;
const API_KEY = process.env.APPWRITE_API_KEY;
const DATABASE_ID = process.env.DATABASE_ID;
const USERS_ID = process.env.USERS_ID;
const SMS_PROVIDER_ID = process.env.SMS_PROVIDER_ID;
const EMAIL_PROVIDER_ID = process.env.EMAIL_PROVIDER_ID;
const PUSH_PROVIDER_ID = process.env.PUSH_PROVIDER_ID;
const GENERAL_TOPIC_ALL = process.env.GENERAL_TOPIC_ALL;
const GENERAL_TOPIC_SMS = process.env.GENERAL_TOPIC_SMS;
const GENERAL_TOPIC_PUSH = process.env.GENERAL_TOPIC_PUSH;
const GENERAL_TOPIC_EMAIL = process.env.GENERAL_TOPIC_EMAIL;
const data = JSON.parse(req.bodyRaw);
const subscribeType = data.type;
try{
TL;DR
Developers are experiencing issues with the createSubscriber endpoint in the Appwrite Cloud Function. The code appears to be getting stuck at a certain point. The issue might be due to handling multiple subscriber creations. They should check the logical flow for each subscribeType and refine the code accordingly, ensuring each messaging.createSubscriber call happens properly. It's also recommended to confirm the functionality with a simpler test scenario.TypeScript
const result = await users.list(
[Query.limit(500)]
);
const usersDocList = result.users;
log(`userList ${usersDocList.length}`);
for(let i=0; i<usersDocList.length; i++){
let usersList = usersDocList[i];
let userId = usersList.$id;
let userPhone = `+${usersList.$id}`;
let userEmail = usersList.email;
let targets = usersList.targets;
TypeScript
if(subscribeType == 'email'){
log(`Email sub`);
log(`UserId ${userId}`);
log(`UserId ${userEmail}`);
log(`targets ${targets.length}`);
for(let j= 0; j<targets.length; j++){
log(`email ${j}`);
if(targets[j].providerType == 'email'){
log(`started`); //================================================== IT GET STUCK HERE
await messaging.createSubscriber(
GENERAL_TOPIC_EMAIL, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
log(`done`);
}
}
}
TypeScript
for(let j= 0; j<targets.length; j++){
if(targets[j].providerType == 'sms'){
await messaging.createSubscriber(
GENERAL_TOPIC_SMS, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}
}
}
else if(subscribeType == 'push'){
for(let j= 0; j<targets.length; j++){
if(targets[j].providerType == 'push'){
await messaging.createSubscriber(
GENERAL_TOPIC_PUSH, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}
}
}
else if(subscribeType == 'all'){
for(let j= 0; j<targets.length; j++){
if(targets[j].providerType == 'email'){
await messaging.createSubscriber(
GENERAL_TOPIC_ALL, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}
if(targets[j].providerType == 'sms'){
await messaging.createSubscriber(
GENERAL_TOPIC_ALL, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}
if(targets[j].providerType == 'push'){
await messaging.createSubscriber(
GENERAL_TOPIC_ALL, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}
}
}
}
}catch(appwriteErrors){
error(appwriteErrors);
}
return res.send({"data": "end like this"});
};```
now, i wrap the subscribe endpoint into try and catch, here is what i'm getting now
TypeScript
log(`Email sub`);
log(`UserId ${userId}`);
log(`UserId ${userEmail}`);
log(`targets ${targets.length}`);
for(let j= 0; j<targets.length; j++){
log(`email ${j}`);
if(targets[j].providerType == 'email'){
log(`started`);
try{
await messaging.createSubscriber(
GENERAL_TOPIC_EMAIL, // topicId
ID.unique(), // subscriberId
targets[j].$id // targetId
);
}catch(creatingError){
error({
"error":creatingError,
"depth":'creatingErrorEmail'
});
}
log(`done`);
}
}
}```
Appwrite Cloud Function - createSubscriber EndPoint not working
Recommended threads
- Custom API domain is unreachable
Earlier my custom api domain was working fine. Now it seems to be offline without a trace a few hours later. I didn't change anything, all the relevant DNS reco...
- "Invalid console fingerprint" when unpau...
I've tried logging out and logging back in, still can't figure out why this is happening.
- executeFunction intermittently throws Fo...
Environment: Flutter app using the Appwrite Flutter SDK, calling executeFunction for [describe endpoint, e.g. live-stream-related function]. *Description*: Int...