mongodb issue when using .find() - node.js

I am using MongoDB for the first time and I came across an error that I could not solve.
Here is my code:
const mongoose = require('../../common/services/mongoose.service').mongoose;
const Schema = mongoose.Schema;
const functions = require('./functions');
const userSchema = new Schema({
email: String,
password: String,
number: String,
state: Boolean
});
userSchema.virtual('id').get(function () {
return this._id.toHexString();
});
// Ensure virtual fields are serialised.
userSchema.set('toJSON', {
virtuals: true
});
userSchema.findById = function (cb) {
return this.model('Users').find({id: this.id}, cb);
};
const User = mongoose.model('Users', userSchema);
exports.findByEmail = (email) => {
//console.log('hello'); //this works btw
return User.find({email: email});
};
So in the code above, findByEmail works fine since hello is logged. However, when it gets to return User.find, I get this error: SyntaxError: Unexpected token g in JSON at position 0.
This is what is currently in the DB:
{
"_id" : ObjectId("6026b813f1828a51f8979616"),
"email" : "gogo#gmail.com",
"password" : "GLImDln1xMKfKS/E99s8qg==$Es3PIHD95vV89973Xq4RecveYMEf22PCH/pFtG1+xq4Gtc4DelA/JXlRNcOR11Rfv/J1uaZCuOplsEmHhY0ehQ==",
"number" : "6969",
"state" : true,
"__v" : 0
}
I input gogo#gmail.com. Does anyone know what I am doing wrong? It would mean a lot to me. I am using node/vue btw.
Error occurs within findByEmail when I run it

Related

Why this node.js code is not working properly

the Node js code written below should give some error and stop but it compile properly and give unexpected product.
the code below doest not throwing error instead it runs and give output of two line like below:-
santu
connnection estabished
and then it stops
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/mynewdb').
then(()=>{console.log("connnection estabished")})
.catch((err)=>{console.log(err)});
//create a schema
const createschema = new mongoose.Schema({
name : {
type : String
},
email:{
type : String,
validate(value){
console.log(value);
if(validator.isEmail(value)===false){
throw new Error('invalid Email');
}
}
}
})
//creating a collection/model
const mycollection1 = new mongoose.model("mycollection1",createschema);
const insertdata = async()=>{
try{
const doc1 = new mycollection1({
name : "santu1",
email : "santu"
})
const mydata = await doc1.save();
console.log(mydata);
}
catch{(err)=>{
console.log(err);
}}
}
insertdata();
The validate() function for the email field in the schema definition needs to return a boolean, but you're throwing an error instead. Try this:
validate(value){
return validator.isEmail(value)
}

Mongoose MongoError : 11000

I sent a create post signal through Postman. a once time, the post signal was a successfully. but next time it was failed . error message is like this.
Error creating new record : {
"driver": true,
"name": "MongoError",
"index": 0,
"code": 11000,
"keyPattern": {
"RoutineParts.userId": 1
},
"keyValue": {
"RoutineParts.userId": null
}
}
i cant understand the error message,
my post code and user model code is like that ,
// it's post code.
router.post('/',(req,res)=>{
const newRecord = User ({
username : req.body.username,
email : req.body.email,
password : req.body.password
})
newRecord.save((err,docs)=>{
if(!err) {
res.send(docs)
}else {
console.log('Error creating new record : ' + JSON.stringify(err,undefined,2))
}
})
})
// it's user model
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema(
{
username : {type:String},
email: { type: String, required: true},
password: { type: String, required: true, trim: true },
created_at : { type: Date, default: Date.now },
updated_at : { type: Date, default: Date.now },
}
)
const User = mongoose.model('User', userSchema);
module.exports = { User }
i can't understand. In fact 'RoutineParts' is a one of model, but i didn't write user's documents. and, controller path is corrected in app.js
how can i solve it?
it's RoutineParts model
const mongoose = require('mongoose')
const userSchema = new mongoose.Schema(
{
routine_name : {type:String},
userId : { type: String, required: true},
exercise_name : { type: String},
order : { type: Number},
}
)
const RoutineParts = mongoose.model('RoutineParts', userSchema);
module.exports = { RoutineParts }
and it's app.js contents
// Connecting data to the MongoDB through Mongoose
require('./db')
const express = require('express')
const app = express()
const PORT = 5000
const bodyParser = require('body-parser')
const userRoute = require('./controller/user')
const routineRoute = require('./controller/routine')
const RP_Route = require('./controller/routineParts')
const EX_Route = require('./controller/excersise')
app.use(bodyParser.json())
app.get("/", function (req, res) {
res.status(201).send("<h1>Hey guys! Hello World !!</h1>");
});
app.listen(PORT, function () {
console.log(`start express server on port ${PORT}`);
});
app.use('/Users', userRoute)
app.use('/Routine', routineRoute)
app.use('/Excersise', EX_Route)
app.use('/RoutineParts', RP_Route)
What's going on
I took a look at your full code. The only real problem, which also fits with the error message you are seeing, is the following :
In your ./models/routineParts.js you've set a unique field. In your case userid. E.g. if your create a new routineParts document with userid "AAA" you can not create another document with the same userid "AAA". In short this means, every user can only create 1 single routineParts document.
The first time you did a POST request to your route localhost:5000/RoutineParts it created the first routineParts document. After that, every request will fail, because it already created 1 routineParts document. ( Read here about unique index with mongoose )
See your ./controller/routineParts.js . If you try to do the same request with a different userid it should work.
How to fix
1 : Remove unique: true from your ./models/routineParts Schema.
2 : ⚡ Drop the index . Mongoose most-likey already registered this index and you have to drop it specifically. Otherwise it will always treat the userid field as unique.
3 : You are still in development, so it shouldn't hurt to also drop the RoutineParts collection.
4 : Restart the app and try to hit the POST localhost:5000/RoutineParts endpoint a couple of times. You now should be able to create multiple routineParts documents under the same user.
How to drop the index
That's a different story. This question should help you with that. They are also using mongoose. If your are using the latest version of mongoose there is a method called cleanIndexes. Use this method after you've removed the unique:true from your model.
For solve this isuse you must remove index duplicate email_1 from index menu

Weird behavior with mongoose Schema $inc operator

I'm working a todo app, it has projects and each project has todolistsand todoListsCount .
When user creates a new todoList I want to increment the project's todoListsCount, the problem I found is that somehow after creating the first todoList the project's todoListsCount successfully increment but the new value is 2 I suppose to what I was expecting which is 2 :
let project= null
await Project.findOneAndUpdate(
{_id:projectId},
{ $inc:{ todoListsCount:1 }},
{ new: true},
(err, doc) => {
if (err)console.log("Something wrong when updating data!");
project =doc
}
);
const todoList = new TodoList({
title,
owner,
projectId,
orderInProject : project.todoListsCount,
created_at : new Date(),
progress : 0,
done_at : new Date(),
todos : [],
done : false,
})
await todoList.save()
as you can see in the Project schema default value of todoListsCount is 0
import mongoose from 'mongoose'
const Schema = mongoose.Schema;
const ProjectSchema = new Schema(
{
/..rest of fields
todosCount : {
type : Number ,
required :false ,
default: 0
},
todoListsCount : {
type : Number ,
required :false ,
default: 0
},
}
)
export const Project = mongoose.model('Project', ProjectSchema);
I guess you are using both await and callback for same function, can be the culprit. Try:
let project = await Project.findOneAndUpdate(
{_id:projectId},
{ $inc:{ todoListsCount:1 }},
{ new: true}).exec();

Mongoose returns empty while the same query in mongodb shell works fine

I know maybe this question has been asked quite many times here, I've went through several solutions people came with to similar questions but none of them seemed to help in my case.
I have two collections called users and posts and models for them look like this:
users
var mongoose = require('mongoose').set('debug', true);
var Schema = mongoose.Schema;
var usersSchema = new Schema({
name: {type: String, required: true}
});
var User = mongoose.model('user', usersSchema, 'users');
module.exports = User;
posts
var mongoose = require('mongoose').set('debug', true);
var Schema = mongoose.Schema;
var postsSchema = new Schema({
content: String,
user: {
type: Schema.ObjectId,
ref: 'users',
required: true
}
});
var Post = mongoose.model('post', postsSchema, 'posts');
module.exports = Post;
I'm trying to get the posts of a user using this code:
var Post = require('../models/posts');
...
router.get('/posts/user/:userId', function (req, res, next) {
Post.find({user: req.params.userId}, function (err, posts) {
Post.populate(posts, {path: 'user'}, function(err, posts) {
res.send(posts);
});
});
});
Mongoose debug mode reports that the following query is executed during the request:
posts.find({ user: ObjectId("592e65765ba8a1f70c1eb0bd") }, { fields: {} })
which works perfectly fine in mongodb shell (I'm using Mongoclient) but with Mongoose this query returns an empty array.
The query I run in mongodb shell:
db.posts.find({ user: "592e65765ba8a1f70c1eb0bd" })
The results I get:
{ "_id" : ObjectId("592e66b48f60c03c1ee06445"), "content" : "Test post 3", "user" : "592e65765ba8a1f70c1eb0bd" }
{ "_id" : ObjectId("592e66b98f60c03c1ee06446"), "content" : "Test post 4", "user" : "592e65765ba8a1f70c1eb0bd" }
{ "_id" : ObjectId("592e66bb8f60c03c1ee06447"), "content" : "Test post 5", "user" : "592e65765ba8a1f70c1eb0bd" }
I'm at the very beginning on learning Node.JS and MongoDB, so maybe I've missed something.
Thank you in advance!
As Neil Lunn suggested, I checked the user field type and it was indeed of type String instead of ObjectId so there was a mismatch of types between the data stored in collection and the field type from the query.
I used this code to convert the user field type from String to ObjectId in my collection:
db.getCollection('posts').find().forEach(function (post) {
db.getCollection('posts').remove({ _id : post._id});
tempUserId = new ObjectId(post.user);
post.user = tempUserId;
db.getCollection('posts').save(post);
}
);
Now everything works as expected.

mongoose find not fetching any result

I've simple collection in mongo and a corresponding mongoose model. This collection will only contain one document always. When I run a query in mongoshell it is giving me the result, but when I try to do findOne using mongoose it is not returning any result at all. Can someone help me figure out what is wrong. Below is my code.
Model:
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
lastResetMonth: {
type: Number
},
lastResetWeek: {
type: Number
},
currentFisYear:{
type: Number
}
});
module.exports = mongoose.model('ReserveResetTrack', schema, 'reserveResetTrack');
const ReserveResetTrack = require('../models/ReserveResetTrack');
ReserveResetTrack.findOne({})
.then(trackData => {
return {
lastFisMonth: trackData.lastMonth,
lastFisWeek: trackData.lastWeek
}
});
The above code is always returning nothing but a promise.
This is the only document i've in my collection and this will be the only one for ever
{
"_id" : ObjectId("589271a36bfa2da821b13ce8"),
"lastMonth" : 0,
"lastWeek" : 0,
"currentYear" : 0
}
Use exec() like this:
ReserveResetTrack.findOne({})
.exec() // <--- use exec() here
.then(trackData => {
return {
lastFisMonth: trackData.lastMonth,
lastFisWeek: trackData.lastWeek
}
});

Resources