I’m able to send and receive data fine when I run my Ionic application on my PC, however when I run the command ionic cordova run android/ionic serve --devapp, I consistently get the error:
OPTIONS http://localhost:1234/products/email net::ERR_CONNECTION_REFUSED ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: “Unknown Error”, url: “http://localhost:1234/products/email”, ok: false, …}
I believe it could be due to Mongo connecting to the IP on my machine rather than my device, however I’m unsure on how to change and fix this (I attempted to but the problem still persisted).
Here’s the code where I connect to MongoDB:
const express = require('express');
const bodyParser = require('body-parser');
var cors = require('cors');
const product = require('./routes/product.route'); // Imports routes for the products
const app = express();
app.use(cors({origin: 'http://localhost:8100'}));
// Set up mongoose connection
const mongoose = require('mongoose');
const uri = "mongodb+srv://username:password@name-xxxx.azure.mongodb.net/name?retryWrites=true&w=majority";
mongoose.connect(uri, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: true,
});
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/products', product);
//app.use('/coordinates', product);
let port = 1234;
app.listen(port, () => {
console.log('Server is up and running on port number ' + port);
});
Any tips are greatly appreciated!