You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the latest version of Mongoose (6.0.12), in the circumstances documented in "Test 3" document validation failures are not caught in a try/catch statement.
importmongoosefrom"mongoose";mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser: true});constdb=mongoose.connection;db.on("error",console.error.bind(console,"connection error:"));db.once("open",asyncfunction(){constSchema=mongoose.Schema;constTestSchema=newSchema({url: {type: String,required: function(){returnBoolean(newURL(this.url));},},});constTest=mongoose.model("Test",TestSchema);// No errors heretry{console.log("Test 1");awaitTest.create({url: "http://example.com"});console.log("No error");}catch(error){console.error("Error caught:",error);}// This will fail but will be caught properlytry{console.log("Test 2");awaitTest.create({url: "invalid url"});console.log("No error");}catch(error){console.error("Error caught:",error);}// This will fail but won't be caught properlytry{console.log("Test 3");awaitTest.create({});console.log("No error");}catch(error){console.error("Error caught:",error);}});
The text was updated successfully, but these errors were encountered:
I think it has to do with new URL() as VScode is yelling at me because of it
import*asmongoosefrom"mongoose";mongoose.connect('mongodb://localhost:27017/test');constdb=mongoose.connection;db.on("error",console.error.bind(console,"connection error:"));db.once("open",asyncfunction(){console.log('open')constSchema=mongoose.Schema;constTestSchema=newSchema({url: {type: String,required: function(){returnBoolean(newURL(this.url));},},});constTest=mongoose.model("Test",TestSchema);// No errors heretry{console.log("Test 1");awaitTest.create({url: "http://example.com"});console.log("No error");}catch(error){console.error("Error caught:",error);}// This will fail but will be caught properlytry{console.log("Test 2");awaitTest.create({url: "invalid url"});console.log("No error");}catch(error){console.error("Error caught:",error);}// This will fail but won't be caught properlytry{console.log("Test 3");awaitTest.create({});console.log("No error");}catch(error){console.error("Error caught:",error);}});
@IslandRhythms what error is VS code giving you? I’m guessing it’s complaining about ‘new URL()’ not being caught in a try/catch statement. But of course in this case we want it not to be caught because we want the statement to throw in order to ensure mongoose fails validation.
Using the latest version of Mongoose (6.0.12), in the circumstances documented in "Test 3" document validation failures are not caught in a try/catch statement.
The text was updated successfully, but these errors were encountered: