I'm trying to add notification in my website. I'm new to node.js and service workers. I’m using expressjs for the backend and a Mysql database.
I'm following this tutorial: Beginners guide to Web Push Notifications using Service Workers and everything worked well until I use a real database for the webpush subscription. Datas are correctly registered in my table but when I try to get those datas with SELECT for testing the send part, I got this error:
C:\...\web-push-lib.js:87
throw new Error('You must pass in a subscription with at least '
^
Error: You must pass in a subscription with at least an endpoint.
I know that the problem comes from the following part but I can’t find out how to use the function to get the result of the query.
//route to test send notification
app.get('/send-notification', (req, res) => {
//connect to the pool
pool.getConnection(function (err, connection) {
if (err) throw err; // not connected!
const sub = { subscription: null };
connection.query(`SELECT webpushsub_info FROM webpushsub`, function (err, result, fields) {
// if any error while executing above query, throw error
if (err) throw err;
// if there is no error, you have the result
sub.subscription = result;
const message = 'Inscription done';
webpush.sendNotification(sub.subscription, message)
connection.release();
res.json({ message: 'message sent' });
});
});
})
EDIT
When I log my result It returns this.
RowDataPacket {
webpushsub_info: '{ //that's the column name where I store subscriptions
"endpoint":"https://fcm.googleapis.com/fcm/send/some_endpoint",
"expirationTime":null,
"keys":{
"p256dh":"some key",
"auth":"an other key"
}
}',
...
}
I also tried to log only one element result[0].webpushsub_info
so I got exactly the same as the entry. "The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser." -npm documentation for web-push.