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

时间:2019-04-07 03:04:09

标签: node.js mongodb express mongoose

我正在尝试从MongoDB删除记录。我的快递代码如下所示

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
        });
      });
}

我遇到如下错误

{ 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 },

1 个答案:

答案 0 :(得分:1)

有两个原因,因为kgangadhar表示您不会得到curid,第二个原因是如果您获得curid而不是 您将获得一个stringId的ObjectId

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
    });
  });
}
相关问题