I am trying to make a cloud function that creates a notification object and adds it to the user’s list of UnreadNotifications. Here is the code for that:
Moralis.Cloud.define('sendNotificationToUser', async (request) => {
const query = new Moralis.Query('User')
const ethAddress = request.params.address.toLowerCase()
query.equalTo('ethAddress', ethAddress)
const result = await query.find({ useMasterKey: true })
const user = result[0]
const NotificationCatalog = Moralis.Object.extend('NotificationCatalog')
const notificationObject = new NotificationCatalog()
const unreadNotifications = user.get('UnreadNotifications')
unreadNotifications.push(await notificationObject.save(request.params.notification))
user.set('UnreadNotifications', unreadNotifications)
user.save()
})
It does not work as expected though. The notification does get successfully created in the database on this line:
unreadNotifications.push(await notificationObject.save(request.params.notification))
But the user’s UnreadNotifications never gets updated, it remains empty. Can someone tell me why this is? Do I need to use master key again somewhere?