Home > Mobile >  Old aunt beginners small program consult, cloud function, to traverse a collection, how to write
Old aunt beginners small program consult, cloud function, to traverse a collection, how to write

Time:12-09

//cloud function entry file
Const cloud=the require (' wx - server - SDK)

Cloud. The init ();
Const db=cloud. The database ();

Const userda=db. Collection (' users');

Const template_id='Rq5eGHbeyzlS8KIdqySLtpXtf8nCftcC3c_CdvR15_s';//small program template message template id
//cloud function entry function
Exports. The main=async (event, the context)=& gt; {
Const wxContext=cloud. GetWXContext ()
Var d=new Date ();
Var dateshow=d.g etFullYear () + '-' + (d.g etMonth () + 1) + '-' + d.g etDate ();

Const getInfo=await userda. The get ();
Const listnum=await userda. The count ();
For (the let I=0; I & lt; Listnum; I++) {
Let touser=getInfo [I] userid;
//send template messages
Try {
Const result=await cloud. Openapi. SubscribeMessage. Send ({
Touser: touser.
Page: 'pages/write/write',
Lang: 'zh_CN',
Data: {
Phrase1: {
Value: 'diary to remind'
},
Thing2: {
Value: 'your diary submission today? If not please submit '
},
Time3: {
Value: dateshow
},
Name4: {
Value: 'system remind'
}
},
TemplateId: template_id,
MiniprogramState: 'developer'
})
Return the result
{} the catch (err)
Return err
}
}
}

===============================================

Give a fixed value touser before is no problem, now want to send information to everyone in collection is

  • Related