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
// 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{
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;
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`);
}
}
}
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
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
- Got message for auto payment of 15usd fo...
how did this happen? 1. i claimed my 50usd credits via jsm hackathon - https://hackathon.jsmastery.pro/ 2. it asked me which org. to apply the credits on, i se...
- Apple OAuth Scopes
Hi Hi, I've configured sign in with apple and this is the response i'm getting from apple once i've signed in. I cant find anywhere I set scopes. I remember se...
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...