使用 express.js 和 react.js 从 MongoDB 中删除记录
2019-04-07
1723
我正在尝试从 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个回答
可能有两个原因,正如 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
});
});
}
Narendra Chouhan
2019-04-07