net::ERR_CONNECTION_REFUSED Sending and receiving data on Ionic application on android device (Mongo)

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!

Hello Nick.

There is a typo in your code. I am not sure if this is in the live version you are using, or if this was just input when posting to the form, but fix it anyway.

Hope this helps.

Hi Sky, I can’t seem to find the typo. If you’re referring to the uri then I changed it a bit to conceal the username and password of my Mongo Atlas

This section. It has an apostrophe at the end.

1 Like

My apologies, just fixed it!