Mongoose Version Error and no matching document found for id - node.js

I have a mongoose schema, and I just added the field "imagens" in the original schema.
The field added is:
imagens:[{title:{type: String},savedAs:{type: String},file:{type: String}, thumb:{type: String}}],
Now when I try fill the 'imagens' field and update the collection, I get the next error.
And if I get rid off the line 'item.imagens=imgs' the error is gone.
What am I doing wrong? Is there some fix to this problem?
//Error
{"data":{"message":"No matching document found for id \"5909caeed32a453b537f7966\"",
"name":"VersionError"}, "status":500,
"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback",
"url":"/uploads",
"data":{"file":{"$ngfBlobUrl":
"blob:http://localhost/9c4b0449-1ddd-4e39-ab44-f2e9a21bfd82","$ngfWidth":450,"$ngfHeight":321,
"upload":{},"progress":100},"pacID":"5909caeed32a453b537f7966"},"_isDigested":true,
"_chunkSize":null,"headers":{"Accept":"application/json, text/plain, */*"},"_deferred":{"promise":"..."},"cached":false},"statusText":"Internal Server Error"}
//in my router
Cliente.findById(pac_id, function (err, item) {
if (err) {
return res.status(500).send(err);
} else {
item.imagens=imgs
}
item.save(function (err, data) {
if (err) {
return res.status(500).send(err)
}
if (answers.results.length){
answers.message='Some files was not uploaded'
} else {
answers.message='Files were uploaded'
}
res.send(answers)
})
})
//model
const mongoose=require('mongoose');
const clientesSchema = new mongoose.Schema({
id: {type: Number, unique:true},
nome: {type: String, unique:true},
ativo: {type: Boolean},
...
...
foto: { data: Buffer, contentType: String },
imagens:[{title:{type: String},savedAs:{type: String},file:{type: String}, thumb:{type: String}}],
created_at:{type:Date,default:Date.now},
altered_at:{type:Date,default:Date.now}
});
module.exports = mongoose.model('Cliente', clientesSchema,'clientes' );

Delete the document version, that will resolve the version conflict.
Then Mongoose will allow you to save :
delete item.__v
item.save(...)

Related

Unable to find index for $geoNear query error with mongoose

I have the following codes which try to create secondary indexes with mongoose. I have followed the mongoose official document to implement it ( mongoose documentation: Indexes section). However, when I send a GET request through Postman, an error, "unable to find index for $geoNear query", occurs. My understanding is that in my case, location is equivalent to a $geoNear object, so my code should be fine (I know it's not fine. That's why I have got an error). Any comments or suggestions would be greatly appreciated.
app.js(get endpoint)
app.get('/api/stores', (req, res) => {
const zipCode = req.query.zip_code;
const googleMapsURL = "https://maps.googleapis.com/maps/api/geocode/json";
axios.get(googleMapsURL, {
params: {
address: zipCode,
key : "KEY"
}
}).then((response) => {
const data = response.data
const coordinates = [
data.results[0].geometry.location.lng,
data.results[0].geometry.location.lat,
]
Store.find({
location: {
$near: {
$maxDistance: 3218,
$geometry: {
type: "Point",
coordinates: coordinates
}
}
}
}, (err, stores)=> {
if (err) {
console.log(err);
res.status(500).send(err);
} else {
res.status(200).send(stores);
}
})
}).catch((error)=> {
console.log(error);
})
})
store.js
const mongoose = require('mongoose');
const storeSchema = mongoose.Schema({
storeName: String,
phoneNumber: String,
address: {},
openStatusText: String,
addressLines: Array,
location: {
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
}
})
storeSchema.index({ location : "2dsphere"}, {sparse: true});
module.exports = mongoose.model('Store', storeSchema);
When creating a new index in MongoDB, you may have to drop the table as to have the index apply properly. Try creating the index on a fresh table. Does that work?
I figured out this error by first creating a new database and collection(I was using the default database named <dbname>). When I sent a POST request with the data I would like to store in MongoDB, MongoError: Can't extract geo keys appeared. I fixed this error by following this thread(reference). After these steps, my GET request worked and indexes were successfully created.

Store value of a subquery - mongoose

What im doing:
When I call getData() the backend server .find() all my data.
My documents:
My test document has an _id a name and stuff fields. The stuff field contains the _id to the data document.
My data document has an _id and a age field
My goal:
When I send the data to the frontend I don´t want the stuff field to appear with the _id, I want it to appear with the age field from the correspondingdata.
What I have:
router.route('/data').get((req, res) => {
Test.find((err, aval) => {
if (err)
console.log(err);
else{
var result = [];
aval.forEach(e => {
var age;
// Get the age, only 1
Data.findById(e.stuff, 'age', function (err, a) {
age = a.age;
});
result.push({name: e.name, age: age});
});
res.json(result);
}
});
});
I find all the test documents then, for each one of them, I find the age and put the result in the array. Finaly I send the result array.
My problem:
The age field on my result array is always undefined, why? Any solutions?
UPDATE 1 - The schemas
The test schema
var TestSchema = new Schema(
{
stuff: {type: Schema.Types.ObjectId, ref: 'Data', required: true},
name: {type: String, required: true}
}
);
The data schema
var DataSchema = new Schema(
{
age: {type: Number, required: true}
}
);
router.route('/data').get((req, res) => {
Test.find({})
.populate('stuff')
.exec((err, aval) => {
if (err) console.log(err);
res.json(aval);
});
});
Mongoose model has a populate property that uses the value in the model attribute definition to get data matching the _id from another model.
It's a scop problem with your code try this out :
Data.findById(e.stuff, 'age', function (err, a) {
result.push({name: e.name, age: a.age});
});
But as a better solution think to use the Aggregation Framework

Mongoose can't save document to the database for one particular collection

I have a function
function generateInvoice(data) {
const cdate = new moment.tz('GMT').toDate();
let invoiceData = {
date: cdate,
paidStatus: true,
amount: data.amount,
userId: data.userId
}
if (data.planName && data.planTypeName) {
invoiceData.item = `${data.planName} - ${data.planTypeName}`
invoiceData.quantity = data.seats || data.slots;
}
if (data.credits) {
invoiceData.item = 'Credits';
invoiceData.quantity = data.credits;
}
return Invoice.create(invoiceData).then((data)=>{
data.invoiceId = data._id.toString().slice(-5);
return data.save().then((data)=>{console.log(data); return data.invoiceId}).catch((err)=>{
throw new ErroWithStatusCode(500, 'Sorry, we seem to be facing some issue right now. Please, try again later.', err);
})
})
}
and this is how I am using this function
return generateInvoice(invoiceData).then((data)=>{
newBooking.orderId = data;
id = data;
return newBooking.save().then((booking) => {
return booking;
}).catch((err) => {
throw new ErroWithStatusCode(500, 'Sorry, we are facing some issue right now. Please try again later.')
})
});
The issue is that I can't find invoiceData in my invoices collection. The data in the callback function of then block is the document, but I can't find the same document in the invoices collection.
All the promises are getting resolved. It is not falling in the catch block, I am receiving valid data from Invoice.create(invoiceData).
Also, newBooking.save() function is working as expected. So, that's what is bothering me the most, as to why is it not working for one specific collection.
What could be the reason behind it?
EDIT: This is the invoice schema
const InvoiceSchema = new Schema({
item: String,
paidStatus: Boolean,
quantity: String,
amount: Number,
invoiceId: String,
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
date: {type: Date, default: Date.now()},
__v: {type: Number, select: false}
}, {strict: true})
export default mongoose.model('Invoice', InvoiceSchema);
And I am not receiving any error, the booking is successful. I tried logging the data received in the then block, and it is a valid document, as I have already mentioned above.
EDIT: The following is the complete code that invovlves the use of generateInvoice function: book.js
Insted of
return Invoice.create(invoiceData).then((data)=>{...
Try
new Invoice(invoiceData).save((err,data)=>{
if(err) return console.log(err);
return data._id;
})

Catch error when using populate with mongoose

I have the next model and route with mongoose:
In my colection I have some invalids id's to "cidade" field and this is why I am getting the error showing below.
The error happens in the line:
.populate('cidade')
Is there a way to execute my router(code is below) in:
router.get('/:id',function(req,res,next){ .....
without stop on that error?
If an invalid "id" is found, I´d just like to ignore it and proceed to next.
My collections are too big and can have some invalids "ids" to "cidade" field.
//error
angular.js:14328 Possibly unhandled rejection: {"data":{"message":"Cast to ObjectId failed for value \"Ararendá\" at path \"_id\" for model \"Cidade\"","name":"CastError","stringValue":"\"Ararendá\"","kind":"ObjectId","value":"Ararendá","path":"_id"},"status":500,"config":
//models and route
//cidade
cidadesSchema = new mongoose.Schema({
uf: {type: String, unique:true},
cidade: {type: String, unique:true}
});
module.exports = mongoose.model('Cidade', cidadesSchema,'cidades' );
//profiss
var profissionaisSchema = new mongoose.Schema({
nome: {type: String, unique:true},
cidade: {type:mongoose.Schema.Types.ObjectId, ref:'Cidade'},
estado: {type:mongoose.Schema.Types.ObjectId, ref:'Estado'},
cep: {type: String},
});
module.exports = mongoose.model('Profissional', profissionaisSchema,'profissionais' );
//route
const callback=function(err,data,res){
if (err) return res.status(500).json(err);
return res.status(200).send(data);
}
router.get('/:id',function(req,res,next){
const query=req.params.id;
Profissional.findById(query).populate('profissao')
.populate('cidade')
.exec( (err,data) => {
callback(err,data,res)
});
});
I don't think you can tell Mongoose to just ignore those errors and keep going, so you're going to have to implement the population yourself (which should be relatively easy because you're using findById which would only yield, at most, one document).
Here's some (untested) code:
Profissional.findById(query).populate('profissao').exec( (err, profi) => {
if (err) {
return res.status(500).json(err);
} else if (! profi || ! /^[a-f0-9]{24}$/i.test(profi.cidade)) {
return res.status(200).send(profi);
}
Cidade.findById(profi.cidade).exec((err, cidade) => {
if (err) {
return res.status(500).json(err);
}
profi.cidade = cidade;
return res.status(200).send(profi);
});
});
If the cidade property looks like a valid ObjectId, it will run a query to retrieve it, otherwise it won't bother.

Removing a blog reference from a tag

I'm working on an application in Node with Mongoose where you're able to post blog entries and tag them. When a blog entry is deleted, I want to remove it's reference from the blog, and here's where I need help.
Below is the route for deleting a blog entry, but I get "TypeError: Cannot call method 'find' of undefined" when I try to delete a blog entry, so I guess my code below is wrong.
app.post('/blog/delete/:id', function(req, res){
model.BlogPost.findById(req.params.id, function (err, blog){
if (err) {
console.log(err);
// do something
}
blog.remove(function(err) {
console.log(err);
// do something
});
var query = model.Tag.find( { blogs: { $in : blog } } );
query.exec(function (err, tags) {
if (err) {
console.log(err);
// do something
}
tags.remove();
res.redirect('back');
});
});
});
Model for blog entries:
var BlogPostSchema = new Schema({
name : String,
type : String,
author : ObjectId,
title : String,
body : String,
buf : Buffer,
date: { type: Date, default: Date.now },
comments : [CommentSchema],
meta : {
upvotes : Number,
downvotes : Number,
// points : { type Number, default: },
favs : Number,
uniqueIPs : [String],
tags : [String]
}
});
modelObject.BlogPost = mongoose.model('BlogPost', BlogPostSchema);
Model for tags:
var TagSchema = new Schema({
name : String
, blogs : [String]
});
modelObject.TagSchema = TagSchema;
modelObject.Tag = mongoose.model('Tag', TagSchema);
Hard to tell with out line numbers, but looks like model.Tag may be undefined.
side note: you probably don't want to remove the tags unless the blog was found and removed successfully.

Resources