Mongoose updates a field with null value throws exception - node.js

Here is the model definition:
pricing: { type: Number, default: null }
this is the exception I get when Mongoose tries to update a record with the source data being null:
Error message I got:
message: 'Cast to number failed for value "NaN" at path "pricing"',
name: 'CastError',
type: 'number',
value: NaN,
path: 'pricing'
I do need to update the existing value with null for this case since the application treat the field to be a null-able Number field.
How to fix it? Thanks in advance!

My guess is that it is trying to cast null to a Number. Try setting the default to 0;

Inserting null values for pricing seems to work okay for me; see the sample code I used below:
app.js
var mongoose = require("mongoose"),
Widget = require("./model.js").model;
// connect to mongo
mongoose.connect("mongodb://localhost/testdb");
// test insertion
var testValid = new Widget({ name: "Valid Test", price: 10.00 }),
testInvalid = new Widget({ name: "Invalid Test", price: null });
testValid.save();
testInvalid.save(function (err) {
if (err) {
console.log(err);
}
});
model.js
var mongoose = require("mongoose");
var schema = new mongoose.Schema({
name: String,
price: { type: Number, default: null }
});
exports.model = mongoose.model("Widget", schema);
exports.schema = schema;
Judging by your error message, it would appear that an invalid calculation is being made, the result of which is attempting to insert into Mongo. "value" in the error message is the value that was trying to be inserted. You can verify this by attempting to save the following document:
var testInvalid = new Widget({ name: "Invalid Test", price: "abc"});
Which will result in:
{ message: 'Cast to number failed for value "abc" at path "price"',
name: 'CastError',
type: 'number',
value: 'abc',
path: 'price' }
Are they any more details or code samples you could share?
EDIT:
I would try isolating the update from the rest of your code and seeing if you can duplicate the error, like this:
var mongoose = require("mongoose"),
Widget = require("./model.js").model;
// connect to mongo
mongoose.connect("mongodb://localhost/testdb");
// insert record
var testInvalid = new Widget({ name: "Invalid Test", price: 10.00 });
// update record
testInvalid.save(function (err) {
if (err) {
console.log(err);
} else {
Widget.findOne({ name: "Invalid Test" }, function (err, record) {
// set price to null & save
record.price = null;
record.save(function (err) {
if (err) {
console.log(err);
}
});
});
}
});
This isolated update appeared to work for me. Sorry I can't be of more help :/

NaN != null is the best way to describe the problem.
You should check the value of the price you are trying to insert for 'NaN'.
If this tests to true then insert a null in your document, otherwise insert the correctly parsed pricing (as a Number).

Related

avoid duplicates on MongoDB using Mongoose

Hi I'm new to MongoDB and Moongoose I'm trying to avoid my api's users to store on the Mongo database duplicated contact's name but seems like it's not working at all.
This is how I'm trying to do it right now the name and the phone number are mandatory and also the name must be unique otherwise it should throw an error.
const contactSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true
},
number: {
type: Number,
required: true
}
});
app.post('/api/persons', (request, response) => {
const body = request.body;
const person = new Contact({
name: body.name,
number: +body.number
});
person.save()
.then(saved => {
response.json(saved);
})
.catch(error => {
return response.status(400).json({
error: 'content missing'
});
});
})
If I send a post request with missing name or number it already throws an error but seems like it's not gettin the unique value validation.
Finally found a package that allows me to avoid duplicted entries on Mongo. I used this package following the documentation instructions:
https://github.com/blakehaswell/mongoose-unique-validator#readme
This is the code I had to write:
const uniqueValidator = require('mongoose-unique-validator');
const contactSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true
},
number: {
type: Number,
required: true
}
});
contactSchema.plugin(uniqueValidator);
the error of unique validation is weird so you can use unique-validator plugin, after that when you send a post request with missing name or number, the error is about required: true
Refer to validation
Validators are not run on undefined values. The only exception is the required validator.
Since both the fields(name and number) in your DB are required.
Instead of directly passing the request body to the query, you can do something like this.
const name = body.name;
const number = body.number;
if(!name || !number) {
// Return response saying either of the fields is empty.
// It's not a good practice to hit the DB with undefined values.
}
let personDetails = {
"name": name,
"contact": contact
};
const person = new Contact(personDetails);
Regarding the unique validation either you can use the unique-validator plugin as suggested by Mohammad Yaser Ahmadi or you can make a DB call to check if the name and number are unique and then hit the save method if that's is feasible for your database.
If you want both the fields name and number to be combined unique you can create Compound Index as follows:
contactSchema.index({ name: 1, number: 1 }, { unique: true });
You can read more on Compound Indexes here: https://docs.mongodb.com/manual/core/index-compound/

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.

Mongoose saves invalid data without throwing validation errors if model.validate() is called first

MongoDB 4.2.2 and Mongoose 5.8.3 (latest) and NodeJS 13.3.0 (Windows x64)
If I create a schema and model, then create an instance of the model and add some data, then run validate(), then save(): even if validate() fails, the data is saved into the collection, without throwing an additional validation error.
Is this a bug, or am I doing something wrong?
Here's the test code:
var mongoose = require('mongoose')
mongoose.connect("mongodb://user:pass#localhost/mydb")
db = mongoose.connection
var Schema = mongoose.Schema
var PartSchema = new Schema({
name: {
type: String,
required: true,
validate: {
validator: (v) => v !== 'asdf' // Don't allow name to be 'asdf'
}
},
number: {
type: String,
required: true,
validate: {
validator: (v) => !v.includes(' ') // Don't allow spaces in part number.
}
}
})
var ProductSchema = new Schema({
name: String,
parts: [PartSchema]
})
var Part = mongoose.model('Part', PartSchema)
var Product = mongoose.model('Product', ProductSchema)
var p1 = new Product({name:"Baseball Bat", parts:[ new Part({name:"First part", number: "003344"}), new Part({name: "Second part", number: "554422"}) ]})
p1.parts.push(new Part({name: "No number, so invalid"})) // this one is invalid because no part number is specified (required)
p1.parts.push(new Part({name: 'asdf', number: 'zzzzzaaaa'}))
p1.parts.push(new Part({name: 'bbbb', number: 'with a space'})) // This one is invalid because number has spaces.
p1.validate()
.then(() => {console.log('Validation successful')})
.catch((err) => { console.log("Validation failed.")})
p1.save()
.then(()=>{ console.log("Saved successfully")})
.catch((err)=>{console.log("Save ERROR", err)})
Running this code yields the following:
Validation failed.
Saved successfully
And the new document appears in the database:
However, if I remove the p1.validate() before calling save(), the save function's catch() block triggers and the item is not saved:
Save ERROR Error [ValidationError]: Product validation failed: parts.2.number: Path `number` is required., parts.3.name: Validator failed for path `name` with value `asdf`, parts.4.number: Validator failed for path `number` with value `with a space`
at ValidationError.inspect
... snipped
May be you need to use p1.save() inside the promise chain.
p1.validate()
.then(res => {
console.log("Validation successful");
})
.then(() => {
return p1.save();
})
.then(res => {
console.log("saved success ", res);
})
.catch(err => {
console.log("Some error.", err);
});

Trouble manually setting the model property using mongoose

I have a simple model, which is:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var citySchema = new Schema({
name: { type: String, required: true },
state: { type: Schema.Types.ObjectId, ref: 'State' }
});
module.exports = mongoose.model('City', citySchema);
Only i access a route, asking to insert a city, giving as post params
POST: {
name: 'My City',
state: 'SE' // Stands for Some State
}
I know the type of the state property is not correct, but in my logic i do:
var newCity = new City(req.body);
if (typeof req.body.state !== 'undefined' && req.body.state.length == 2) {
State.findOne({uf: req.body.state.toUpperCase()}, function(err, foundState) {
if (err) { res.send({status: 500, message: 'Could not find the required state'}); return; }
newCity.state = foundState._id;
newCity.set('state', foundState._id);
return;
});
}
But, once i do a res.send(newCity), to check out the newCity variable properties, it prints:
{
"name": "Balneário Camború",
"_id": "570ff2944c6bd6df4e8e76e8"
}
And if i try to save it, i get the following error:
ValidationError: CastError: Cast to ObjectID failed for value \"SE\" at path \"state\""
So, i'm quite confused, because when the Model is created using the req.body properties, it does not list the state property, even if i set it later in the code, but when i try to save the City, it throw an error of mistype.
What could be causing this, and how should i procede?

Mongoose findOneAndUpdate return invalidate error

I'm trying to add comments to this scheme:
var CommentSchema = new Schema(
{
ad_id:String,
comments:
[
{
author: String,
authorID: Number,
posted: Date,
text: String,
title: String
}
]
}
But if the ad_id exists i just want to push the new comment to comments basically create a upsert query:
var query = {'ad_id': req.body.data.ad_ID}
var doc = {$set:{'ad_id':req.body.data.ad_ID},$push:{'comments':{'author':req.body.data.author,'authorID':req.body.data.uID,'posted':req.body.data.posted
,'text':req.body.data.text, 'title':req.body.data.title}}};
var options = {upsert:true};
Comments.findOneAndUpdate(query,doc,options, function (err, doc) {
if(err) {
console.log(String(err));
res.send({"foo": String(err)});
}
else {
console.log((doc));
res.send(doc);
}
});
But in the get the follwing error :
Unable to invalidate a subdocument that has not been added to an array.
It turns out that I had data type error that causes that.
If anyone encounter that error code make sure that the data types match the input.

Resources