Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertMany() make node process to CRASH Unable to catch() #8363

Closed
IndraNyo opened this issue Nov 20, 2019 · 0 comments · Fixed by #8365
Closed

insertMany() make node process to CRASH Unable to catch() #8363

IndraNyo opened this issue Nov 20, 2019 · 0 comments · Fixed by #8365
Milestone

Comments

@IndraNyo
Copy link

Do you want to request a feature or report a bug?

Report Bug

What is the current behavior?

insertMany()

If the current behavior is a bug, please provide the steps to reproduce.

schema: web_image.js

const mongoose = require('mongoose')
const webImageSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  url: { type: String, required: false, unique: false },
  ossurl: { type: String, required: false, unique: false }
},
{
  timestamps: {
    createdAt: 'created',
    updatedAt: 'updated'
  },
  toObject: { virtuals: true },
  toJSON: { virtuals: true },
  writeConcern: {
    w: 'majority',
    j: true,
    wtimeout: 10000
  },
  collection: 'imgs'
})
webImageSchema.set('autoIndex', false)
module.exports = mongoose.model('imgs', webImageSchema)

controller:

const DBWebImage = require('../models/web_image.js')
exports.Web_Image_Add = async (ctx, next) => {
  try {
    const urlArr = ctx.request.body.url_list
    await DBWebImage
      .insertMany(urlArr, { ordered: false })
      .then(res => {
        ctx.status = 201
        ctx.body = { res }
      })
      .catch(error => {
        ctx.status = 500
        ctx.body = { res }
      })
    // }
  } catch (error) {
    ctx.throw(500)
    ctx.body = { error }
  }
}

route:
router.post('/add', WebImageController.Web_Image_Add)

post body RIGHT:

{
	"url_list":[
		{
			"url":"https://image1.ljcdn.com/hdic-frame/prod-d797e592-3f239-43e9-9831-32412433c2fd.png!m_fill,w_500,h_280,l_frame,f_jpg,ls_30",
			"ossurl": "webget/1906/13/7a13bc308d8511e9861200163e0fb691.jpg3"
		},
		{
			"url":"https://image1.ljcdn.com/hdic-frame/prod-d797e592-3f29-43e9-9831-32412433c2fd.png!m_fill,w_500,h_280,l_frame,f_jpg,ls_310",
			"ossurl": "webget/1906/13/7a13bc308d8511e9861200163e0fb691.jpg1"
		},
		{
			"url":"https://image1.ljcdn.com/hdic-frame/prod-d797e592-3f29-43e9-9831-32412433c2fd.png!m_fill,w_500,h_280,l_frame,f_jp2g,ls_310",
			"ossurl": "webget/1906/13/7a13bc308d8511e9861200163e0fb691.jpg12"
		}
	]
}

post body WRONG:

{
	"url_list":[
		"url","ossurl"
	]
}
/Users/indra/server/article-node/node_modules/mongoose/lib/document.js:86
    throw new ObjectParameterError(obj, 'obj', 'Document');
    ^
ObjectParameterError: Parameter "obj" to Document() must be an object, got url
    at new ObjectParameterError (/Users/indra/server/article-node/node_modules/mongoose/lib/error/objectParameter.js:25:11)
    at model.Document (/Users/indra/server/article-node/node_modules/mongoose/lib/document.js:86:11)
    at model.Model (/Users/indra/server/article-node/node_modules/mongoose/lib/model.js:97:12)
    at new model (/Users/indra/server/article-node/node_modules/mongoose/lib/model.js:4425:15)
    at Array.<anonymous> (/Users/indra/server/article-node/node_modules/mongoose/lib/model.js:3212:15)
    at _start (/Users/indra/server/article-node/node_modules/mongoose/lib/helpers/parallelLimit.js:27:37)
    at parallelLimit (/Users/indra/server/article-node/node_modules/mongoose/lib/helpers/parallelLimit.js:23:5)
    at Function.Model.$__insertMany (/Users/indra/server/article-node/node_modules/mongoose/lib/model.js:3233:3)
    at process.nextTick (/Users/indra/server/article-node/node_modules/kareem/index.js:369:33)
    at process._tickCallback (internal/process/next_tick.js:61:11)
[nodemon] app crashed - waiting for file changes before starting...

What is the expected behavior?

Insert data format error will cause the node process to CRASH
AND Unable to catch()

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

node 10.15.0
Mongosse 5.7.12
MongoDB 4.0.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants