I have created a MongoDB database named: "helsinki_city_bike" and I uploaded CSV data using MongoDB compass in the collection name: "journey_detail". I also connected the database to atlas using a connection string. Now that I want to fetch the data from the database it's creating another test db and collection name "journey_detail" inside "helsinki_city_bike".
how can I fetch data from my collection without creating an empty collection?
the function is working well as I am getting an empty array. what I am doing wrong with the model?
This is my schema:
import mongoose from "mongoose";
export const journeySchema = mongoose.Schema({
Departure_time: String,
Return_time: String,
Departure_Station_Id: Number,
Departure_Station_Name: String,
Return_Station_Id: Number,
Return_Station_Name: String,
Covered_Distance: Number,
Duration: Number,
});
export const journey_detail = mongoose.model("journey_detail", journeySchema);
req function:
import { journey_detail } from "../models/Schema.js";
export const getJourneyDetails = async (req, res) => {
try {
const JourneyDetails = await journey_detail.find().limit(10);
if (!JourneyDetails) {
return res.status(404).json({ message: "Journey Details not found" });
}
res.status(200).json(JourneyDetails);
} catch (error) {
console.error(error);
res.status(500).json({ message: "Error retrieving Journey Details" });
}
};
connection:
const app = express();
dotenv.config();
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/", router);
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
app.listen(3001, () => console.log(`server running on the port ${3001}`))
)
.catch((error) => console.log(error));
mongoose.set();
mongoose.connection.on("open", () => {
console.log("Mongoose connected.");
});
Related
I have created a MongoDB database named: "helsinki_city_bike" and I uploaded CSV data using MongoDB compass in the collection name: "journey_detail". I also connected the database to atlas using a connection string. Now that I want to fetch the data from the database it's creating another test db and collection name "journey_detail" inside "helsinki_city_bike".
how can I fetch data from my collection without creating an empty collection?
the function is working well as I am getting an empty array. what I am doing wrong with the model?
This is my schema:
import mongoose from "mongoose";
export const journeySchema = mongoose.Schema({
Departure_time: String,
Return_time: String,
Departure_Station_Id: Number,
Departure_Station_Name: String,
Return_Station_Id: Number,
Return_Station_Name: String,
Covered_Distance: Number,
Duration: Number,
});
export const journey_detail = mongoose.model("journey_detail", journeySchema);
req function:
import { journey_detail } from "../models/Schema.js";
export const getJourneyDetails = async (req, res) => {
try {
const JourneyDetails = await journey_detail.find().limit(10);
if (!JourneyDetails) {
return res.status(404).json({ message: "Journey Details not found" });
}
res.status(200).json(JourneyDetails);
} catch (error) {
console.error(error);
res.status(500).json({ message: "Error retrieving Journey Details" });
}
};
connection:
const app = express();
dotenv.config();
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/", router);
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
app.listen(3001, () => console.log(`server running on the port ${3001}`))
)
.catch((error) => console.log(error));
mongoose.set();
mongoose.connection.on("open", () => {
console.log("Mongoose connected.");
});
I seem to be getting this error:
C:\...\node_modules\mongoose\lib\query.js:4638
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "cProd"
at model.Query.exec (C:\...\node_modules\mongoose\lib\query.js:4638:21)
at model.Query.Query.then (C:\...\node_modules\mongoose\lib\query.js:4737:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
messageFormat: undefined,
stringValue: '"undefined"',
kind: 'ObjectId',
value: 'undefined',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
at new BSONTypeError (C:\...\node_modules\bson\lib\error.js:41:28)
at new ObjectId (C:\...\node_modules\bson\lib\objectid.js:65:23)
at castObjectId (C:\...\node_modules\mongoose\lib\cast\objectid.js:24:12)
at ObjectId.cast (C:\...\node_modules\mongoose\lib\schema\objectid.js:247:12)
at ObjectId.SchemaType.applySetters (C:\...\node_modules\mongoose\lib\schematype.js:1180:12)
at ObjectId.SchemaType._castForQuery (C:\...\node_modules\mongoose\lib\schematype.js:1615:15)
at ObjectId.SchemaType.castForQuery (C:\...\node_modules\mongoose\lib\schematype.js:1605:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\...\node_modules\mongoose\lib\schematype.js:1582:20)
at cast (C:\...\node_modules\mongoose\lib\cast.js:341:32)
at model.Query.Query.cast (C:\...\node_modules\mongoose\lib\query.js:5073:12),
valueType: 'string'
}
for this code:
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const cProd = require('./models/cProd');
mongoose.connect('mongodb://localhost:#/database', {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Database connected');
});
const app = express();
// app.engine('ejs', ejsMate)
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'))
// Parsing request.body
app.use(express.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.render('main')
});
app.get('/cProds', async (req, res) => {
const cProds = await cProd.find({});
res.render('cProds/index', {cProds})
});
app.get('/cProds/create', (req, res) => {
res.render('cProds/create');
})
app.post('/cProds', async (req, res) => {
const conProd = new cProd(req.body.cProd);
await conProd.save();
res.redirect(`/cProds/${cProd._id}`)
});
app.get('/cProds/:id', async (req, res) => {
const conProd = await cProd.findById(req.params.id)
res.render('cProds/show', {conProd})
});
app.listen(3000, () => {
console.log('Serving on port #')
})
The app.post above seems to work when I do a simple res.send(req.body), but I get this error if I finish the code.
Dumb question, is it possibly due to not installing UUID? I don't believe I have yet.
Please help - complete beginner here who has exhausted research and figuring this out.
UPDATE: Schema and Model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const opts = {toJSON:{virtuals:true}};
const CProdSchema = new Schema({
product_name: String,
type: String,
description: String
}, opts);
CProdSchema.virtual('properties.popUpMarkup').get(function() {
return `
<strong>${this.product_name}</strong>
<p>${this.description.substring(0,20)}...</p>`
});
module.exports = mongoose.model('cProd', CProdSchema)
Ah, I think I was blind.
Your problem method:
app.post('/cProds', async (req, res) => {
const conProd = new cProd(req.body.cProd);
await conProd.save();
res.redirect(`/cProds/${cProd._id}`)
});
You want to send the user to the url for the newly created cProd. But cProd is the model name, while conProd is the new document created.
Simply change the last line:
res.redirect(`/cProds/${conProd._id}`)
To avoid the confusion in future, I recommend sticking to capitalized first letter of models. I'm making a guess at the name, so for example:
const CampgroundProduct = require('./models/cProd');
And any instances (documents) of this model can be called campgroundProduct or simply product.
As to how that specific error code happened, I'm guessing that model._id references the creator for that type of field, which would be mongoose.Types.ObjectId()
I want to get access to all the collections of my MongoDB database.
But I am unable to do so.
I am using mongoose.connection.db.getCollection(collection_name) just above the listen part of code but console is saying
mongoose.connection.db.getCollection is not a function.
Here is my code
import express from "express";
import mongoose from "mongoose";
import Messages from "./messages.js";
import dynamicModel from "./messagesRoom.js";
import cors from "cors";
// app configuration
const app = express();
const port = process.env.PORT || 9000;
//middleware
app.use(express.json());
app.use(cors());
// DB Configuration
const url = "mongodb+srv://username:password#cluster0.zp9dc.mongodb.net/Whatsapp_MERN";
mongoose.connect(url, {useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true})
.then(()=> console.log('mongoDB is connected'))
.then(err => console.log(err));
const db = mongoose.connection;
db.once('open', () => {
console.log("DB is connected");
const msgCollection = db.collection('messagecontents');
const changeStream = msgCollection.watch();
changeStream.on('change', (change) => {
console.log(change);
if(change.operationType === 'insert'){
const msgDetails = change.fullDocument;
pusher.trigger('messages', 'inserted',
{
name: msgDetails.name,
message: msgDetails.message,
timestamp: msgDetails.timestamp,
received: msgDetails.received,
})
}
else{
console.log('Error triggering pusher');
}
})
})
// API routes
app.get("/", (req, res) => {
res.status(200).send("Hello World");
})
app.get("/messages/sync", async (req, res) => {
await Messages.find( (err, data) => {
if(err){
console.log(err);
res.status(500).send(err);
}else{
res.status(200).send(data);
}
})
})
app.post("/changeChat", (req, res) => {
const collection_name = req.body.chatName;
let collection = mongoose.connection.db.getCollection("collection_name");
console.log(collection);
})
// listening part
app.listen(port, () => console.log(`listening on port number ${port}`));
please suggest me a way using which I can get access to collections of database according to the name I am using.
mongoose.connection.on('open', function (ref) {
mongoose.connection.db.listCollections().toArray(function(err, names){
console.log(names)
})
})
Try this code block below "mongoose.connect()", the 'listCollections()' function will return the list of all the collection in the respective database and 'toArray()' function will convert that list into array, then we simply log the array.
I am making a webpage which fetches data from mongoDB( atlas BASIC free plan) and has dynamic routing page with SSG. The dynamic routing page, [cogid].js, has getstaticpaths and getstaticprops. getstaticpaths seems work, but getstaticprops does not work. I guess the problem is variable "params.cogid" inside getstaticprops...
import { MongoClient } from "mongodb";
const { MONGODB_URI, MONGODB_DB } = process.env;
export default function Cog({ cogData }) {
return (
<div className="container mx-auto px-2 my-5 flex flex-col ">
<p>COG ID: COG{cogData.cog_id}</p>
<p>name: {cogData.name}</p>
</div>
);
}
export async function getStaticPaths() {
const client = new MongoClient(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
if (!client.isConnected()) await client.connect();
const res = await client
.db(MONGODB_DB)
.collection("test1")
.find({})
.project({ cog_id: 1, _id: 0 })
.toArray();
if (client.isConnected()) client.close();
const paths = res.map((copiedCog) => ({
params: { cogid: copiedCog.cog_id },
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const client = new MongoClient(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
if (!client.isConnected()) await client.connect();
const resp = await client
.db(MONGODB_DB)
.collection("test1")
.findOne({ cog_id: params.cogid });
if (client.isConnected()) client.close();
const cogData = await JSON.parse(JSON.stringify(resp));
return { props: { cogData } };
}
I solved the problem.
I should have changed the data type of params.cogid to Integer (from String?) like Number(params.cogid) or parseInt(params.cogid). It is because the type of the field "cog_id" of the DB is Integer.
I am trying to register the user to mongodb atlas for registration and login but i am geting an error 404.
here is full link to mycode
https://github.com/badrinathareddyr/falcon.git
server.js file
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const authRoute = require('../backend/routes/auth')
//connect to database
dotenv.config({ path: __dirname + '/.env' });
mongoose.connect(
process.env[DB_CONNECT], { useNewUrlParser: true }, () =>
console.log('connected to db!')
);
//Middleware
app.use(express.json());
//middlewareroutes
app.use('/register', authRoute);
app.listen(3000, () => console.log('server up and running'));
auth.js file
var express = require('express');
const router = express.Router();
const User = require('../models/User');
const bcrypt = require('bcryptjs');
router.post('/register', function (req, res) {
if (!req.body.email || !req.body.password) {
res.json({ success: false, msg: 'Please pass email and password.' });
} else {
var newUser = new User({
email: req.body.email,
password: req.body.password
});
// save the user
newUser.save(function (err) {
if (err) {
return res.json({ success: false, msg: 'Email already exists.' });
}
res.json({ success: true, msg: 'Successful created new user.' });
});
}
});
module.exports = router;
It's because of process.env.DB_CONNECT in your code is undefined. Change line 5 of your src/backend/server.js file like below:
const dotenv = require('dotenv').config({ path: __dirname + '/.env' });
then comment dotenv.config()
and copy .env to src/backend/.
Or change line 10 or the file like this:
dotenv.config({ path: __dirname + '/.env' });
You have const User = require('../models/User'); two time in backend/routes/auth.js. Comment line 9. It will give you error.
I fixed it and created pull request in github. Merge it.