-2
I’m trying to register users on Mongodb.
The environment is windws 8.1 and I am also using Mongoose, express and Xios in the backend and Insomnia to test the registration. When I try to register Node.js returns these errors to me:
(node:252) UnhandledPromiseRejectionWarning: MongoError: location object expected, location array not in correct format
at Function.create (C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\core\error.js:44:12)
at toError (C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\utils.js:150:22)
at C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\operations\common_functions.js:265:39
at handler (C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\core\sdam\topology.js:971:24)
at C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\core\sdam\server.js:496:5
at C:\Users\User\Desktop\SemanaOministack\backend\node_modules\mongodb\lib\core\connection\pool.js:420:18
at processTicksAndRejections (internal/process/task_queues.js:76:11)
(node:252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The code looks like this. Routes.js file:
const { Router } = require('express');
const axios = require('axios');
const Dev = require('./models/Dev');
const routes = Router();
routes.post('/devs', async(request, response) => {
const {
github_username,
techs,
latitude,
longitude
} = request.body;
const apiResponse = await axios.get(`https://api.github.com/users/${github_username}`);
const {
name = login, avatar_url, bio
} = apiResponse.data;
const techsArray = techs.split(',').map(tech => tech.trim());
const location = {
type: 'Point',
coordinates: [longitude, latitude],
};
const dev = await Dev.create({
github_username,
name,
avatar_url,
bio,
techs: techsArray,
location,
});
return response.json(dev);
});
module.exports = routes;
Dev.js file:
const mongoose = require('mongoose');
const PointSchema = require('./utils/PointSchema');
const DevSchema = new mongoose.Schema({
name: String,
github_username: String,
bio: String,
avatar_url: String,
techs: [String],
location: {
type: PointSchema,
index: '2dsphere',
}
});
module.exports = mongoose.model('Dev', DevSchema);
index.js file:
const express = require('express');
const mongoose = require('mongoose');
const routes = require('./routes');
const app = express();
mongoose.connect('mongodb+srv://omnistack:[email protected]/week10?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
app.use(express.json());
app.use(routes);
app.listen(3333);
Pointschema.js file:
const mongoose = require('mongoose');
const PointSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
required: true,
},
coordinates: {
type: [Number],
required: true,
},
});
module.exports = PointSchema;
I tried to look at the documentation but I could not handle the errors. IP is released. I put there in the Mongo 0.0.0.0. The register worked normally until I added "Location" on line 27, file Routes.js. If you comment on the line, it runs normally. The problem is there, but I have no idea what I have to do to fix it.
Post your Code so someone can help you.
– Edu Mendonça
Please replace the images with text and don’t forget to format them properly.
– tvdias
have IP limitation on mongodb? has put 0.0.0.0
– rodrigo.oliveira
No limitation. IP released. 0.0.0.0
– César Augusto