zabsalahid / serialport-gsm

SerialPort-GSM is a simplified plugin for communicating with gsm modems. (Primarily for sms) (Focused in PDU mode)
MIT License
88 stars 46 forks source link

ReferenceError: message is not defined #108

Closed Idric974 closed 1 year ago

Idric974 commented 1 year ago

Hi I am Idric.

I use serialport-gsm and it's a great program.

But, I have the following problem when I receive an SMS: ReferenceError: message is not defined How to solve this problem?

INFO :

ReferenceError: message is not defined at EventEmitter.item.logic (/home/pi/Desktop/SMS808/node_modules/serialport-gsm/lib/functions/modem.js:167:31) at /home/pi/Desktop/SMS808/node_modules/serialport-gsm/lib/functions/modem.js:1117:50 at Array.forEach () at /home/pi/Desktop/SMS808/node_modules/serialport-gsm/lib/functions/modem.js:1045:16 at Array.forEach () at SerialPort.modem.dataReceived (/home/pi/Desktop/SMS808/node_modules/serialport-gsm/lib/functions/modem.js:1043:11) at SerialPort.emit (node:events:527:28) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) at SerialPort.Readable.push (node:internal/streams/readable:228:10) [nodemon] app crashed - waiting for file changes before starting...

Thanks in advance.

Idric

spidy0x0 commented 1 year ago

add code

Idric974 commented 1 year ago

Spidyhackx thanks for your reply.

I am a beginner developer.

Your comment should make sense to an experienced developer but to me it doesn't.

Can you be more explicit in your explanation.

This will help me and people who after me will have the same problem.

And so for you, fewer questions…

Can you give me the exact procedure to follow please?

Thank you, Idric Evarne.

spidy0x0 commented 1 year ago

what code are you trying to run?

Idric974 commented 1 year ago

`const express = require('express'); const axios = require('axios'); const app = express(); const bodyParser = require('body-parser'); const { options, gsmModem } = require('./config/config')

//! GESTION DU MODEM.

//? Procedure initialisation du modem.

gsmModem.on('open', () => { // console.log(=====> [ INFO SET MODEM ] Modem ouvert avec succès.); // gsmModem.initializeModem((msg, err) => { // if (err) { // console.log(❌❌❌ Erreur d'initialisation du modem : ${err}); } else { // console.log( =====> [ INFO SET MODEM ] Initialisation du modem : ${JSON.stringify( msg.status )} ); } }); });

//? -------------------------------------------------

//? Reception des SMS.

gsmModem.on('onNewMessage', data => { // console.log("=====> [ INFO SMS RECU ] Numéro de téléphone : ", data['sender']); console.log("=====> [ INFO SMS RECU ] Corps du message : ", data['message']); console.log("=====> [ INFO SMS RECU ] Index du message : ", data['index']); console.log("=====> [ INFO SMS RECU ] msgStatus du message : ", data['msgStatus']); console.log("=====> [ INFO SMS RECU ] dateTimeSent du message : ", data['dateTimeSent']);

//* Requete de retour SMS.

const url = 'http://192.168.1.10:3003/api/postSmsOrderRoute/postSmsOrder';

let message = "TEST SMS ORDER";

axios .post(url, { message, }) .then(function (response) { console.log('SMS ORDERS : ', response.data); }) .catch(function (error) { console.log(error); });

});

//* -------------------------------------------------

//? -------------------------------------------------

//? onSendingMessage.

gsmModem.on('onSendingMessage', data => { //whole message data console.log(Message d'envoi d'événement : + JSON.stringify(data)); });

//? -------------------------------------------------

//? Si mémoire pleine.

gsmModem.on('onMemoryFull', data => { //whole message data console.log(Event Memory Full: + JSON.stringify(data)); });

//? -------------------------------------------------

//? Ouverture du port modem.

gsmModem.open('/dev/ttyAMA0', options);

//? -------------------------------------------------

//! --------------------------------------------------

//! GGESTION DES REQUETES HTTP.

//? Import des routes.

const postSmsRoutes = require('./routes/postSmsRoute'); const getSmsRoutes = require('./routes/getSmsRoute');

//? --------------------------------------------------

//? Utilisation de cors pour les connexions

const cors = require('cors'); app.use(cors());

//? --------------------------------------------------

//? Header pour les Cross Origine

app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization' ); res.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS' ); next(); });

//? --------------------------------------------------

//? Utilisation de body parser

app.use(bodyParser.json());

//? --------------------------------------------------

//? Génération des pages html.

app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); });

app.get('/pageRelay.html', (req, res) => { res.sendFile(__dirname + '/pageRelay.html'); });

//? --------------------------------------------------

//? Les images.

app.use('/images', express.static('/home/pi/Desktop/champiBack_V2/images'));

//? --------------------------------------------------

//? Le CSS.

app.use('/styles', express.static('/home/pi/Desktop/champiBack_V2/styles'));

//? --------------------------------------------------

//? Le Javascript.

app.use('/', express.static('/home/pi/Desktop/champiBack_V2/'));

//? --------------------------------------------------

//? Liste des routes.

app.use('/api/postSms', postSmsRoutes); app.use('/api/getSms', getSmsRoutes);

//? --------------------------------------------------

module.exports = app;`

spidy0x0 commented 1 year ago

your code if full of errors. Why are you logging strings without using quotes " " ? and why is there template literal ` ` around your code ?

gervasek commented 1 year ago

I have the same error, using the example script https://github.com/zabsalahid/serialport-gsm/blob/master/example/example.js

/app/node_modules/serialport-gsm/lib/functions/modem.js:167
          modem.deleteMessage(message)
                              ^

ReferenceError: message is not defined
    at item.logic (/app/node_modules/serialport-gsm/lib/functions/modem.js:167:31)
    at /app/node_modules/serialport-gsm/lib/functions/modem.js:1117:50
    at Array.forEach (<anonymous>)
    at /app/node_modules/serialport-gsm/lib/functions/modem.js:1045:16
    at Array.forEach (<anonymous>)
    at modem.dataReceived (/app/node_modules/serialport-gsm/lib/functions/modem.js:1043:11)
    at SerialPort.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)

Node.js v18.9.0
AviFix commented 1 year ago

Having the same error

This solved the error

modem.deleteMessage({index: id})

but I'm not sure what are the consequences