简体   繁体   English

使用express.js和react.js从MongoDB中删除记录

[英]Delete record from MongoDB using express.js and react.js

I am trying to delete a Record from MongoDB. 我正在尝试从MongoDB删除记录。 My express code is like below 我的快递代码如下

const deleteAddress = (req, res, next) => {
    var curid = req.params.id;
    curid = curid.replace(/\s/g, '');
    Address.findByIdAndRemove(curid)
      .then(result => {
        res.status(200).json({
          message: 'Address Deleted',
          result
        });
      })
      .catch(err => {
        console.log(err);
        res.status(500).json({
          message: 'Error Occured',
          error: err
        });
      });
}

I am getting error like below 我收到如下错误

{ CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Address"
[0]     at new CastError (/home/foysal/Videos/my-app/node_modules/mongoose/lib/error/cast.js:29:11)
[0]     at ObjectId.cast (/home/foysal/Videos/my-app/node_modules/mongoose/lib/schema/objectid.js:242:11)
[0]     at ObjectId.SchemaType.applySetters (/home/foysal/Videos/my-app/node_modules/mongoose/lib/schematype.js:845:12)
[0]     at ObjectId.SchemaType._castForQuery (/home/foysal/Videos/my-app/node_modules/mongoose/lib/schematype.js:1248:15)
[0]     at ObjectId.SchemaType.castForQuery (/home/foysal/Videos/my-app/node_modules/mongoose/lib/schematype.js:1238:15)
[0]     at ObjectId.SchemaType.castForQueryWrapper (/home/foysal/Videos/my-app/node_modules/mongoose/lib/schematype.js:1217:15)
[0]     at cast (/home/foysal/Videos/my-app/node_modules/mongoose/lib/cast.js:307:32)
[0]     at model.Query.Query.cast (/home/foysal/Videos/my-app/node_modules/mongoose/lib/query.js:4340:12)
[0]     at castQuery (/home/foysal/Videos/my-app/node_modules/mongoose/lib/query.js:4192:18)
[0]     at model.Query.Query._findAndModify (/home/foysal/Videos/my-app/node_modules/mongoose/lib/query.js:3204:23)
[0]     at model.Query.<anonymous> (/home/foysal/Videos/my-app/node_modules/mongoose/lib/query.js:3165:8)
[0]     at model.Query._wrappedThunk [as _findOneAndRemove] (/home/foysal/Videos/my-app/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
[0]     at process.nextTick (/home/foysal/Videos/my-app/node_modules/kareem/index.js:369:33)
[0]     at process._tickCallback (internal/process/next_tick.js:61:11)
[0]   message:
[0]    'Cast to ObjectId failed for value "undefined" at path "_id" for model "Address"',
[0]   name: 'CastError',
[0]   stringValue: '"undefined"',
[0]   kind: 'ObjectId',
[0]   value: 'undefined',
[0]   path: '_id',
[0]   reason: undefined,
[0]   model:
[0]    { [Function: model]
[0]      hooks: Kareem { _pres: [Map], _posts: [Map] },
[0]      base:
[0]       Mongoose {
[0]         connections: [Array],
[0]         models: [Object],
[0]         modelSchemas: [Object],
[0]         options: [Object],
[0]         _pluralize: [Function: pluralize],
[0]         Schema: [Function],
[0]         model: [Function],
[0]         plugins: [Array] },
[0]      modelName: 'Address',
[0]      model: [Function: model],
[0]      db:
[0]       NativeConnection {
[0]         base: [Mongoose],
[0]         collections: [Object],
[0]         models: [Object],
[0]         config: [Object],
[0]         replica: false,
[0]         options: null,
[0]         otherDbs: [],
[0]         relatedDbs: {},
[0]         states: [Object],
[0]         _readyState: 1,
[0]         _closeCalled: false,
[0]         _hasOpened: true,
[0]         '$internalEmitter': [EventEmitter],
[0]         _listening: false,
[0]         _connectionOptions: [Object],
[0]         name: 'addresses',
[0]         host: 'localhost',
[0]         port: 27017,
[0]         user: undefined,
[0]         pass: undefined,
[0]         client: [MongoClient],
[0]         '$initialConnection': [Promise],
[0]         _events: [Object],
[0]         _eventsCount: 2,
[0]         db: [Db] },
[0]      discriminators: undefined,
[0]      events:
[0]       EventEmitter { _events: {}, _eventsCount: 0, _maxListeners: undefined },

there can be two reason, as kgangadhar said you would have not getting the curid , second if you are getting the curid than you have pass as a ObjectId you would have getting a stringID 可能有两个原因,如kgangadhar所述,您将不会获得curid ;其次,如果您获得的是curid不是您以ObjectId的身份通过,则将获得stringID

var ObjectId = Schema.ObjectId 

const deleteAddress = (req, res, next) => {
   var curid = new ObjectId(req.params.id);
   curid = curid.replace(/\s/g, '');
   Address.findByIdAndRemove(curid)
     .then(result => {
        res.status(200).json({
        message: 'Address Deleted',
        result
     });
   })
   .catch(err => {
      console.log(err);
      res.status(500).json({
      message: 'Error Occured',
      error: err
    });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM