所以我试图更新我的数据库中的一个字段以每次都增加它。像一个计数器,以便我不断获得更新的值。为此,我正在使用这个
var CounterSchema = new Schema({
_id: String,
_const: String,
count: Number
});
然后我创建了我的模型
const Counter = mongoose.model("counter", CounterSchema);
然后我正在使用一个函数来更新我的集合中的一个字段,例如以下......
async function getNextSequence() {
var count = await Counter.findOneAndUpdate(
{ _const: "some" },
{ $inc: { count: 1 } },
{ new: true }
);
return count.count;
}
但是在运行上述函数时会出现一些错误。我已经确定给定的 _id 存在,这是我得到的错误……
(node:10788) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
9:58 PM
{ ValidationError: Url validation failed: counter: Cast to Number failed for value "Promise { <pending> }" at path "counter"
9:58 PM
at ValidationError.inspect (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/error/validation.js:61:24)
9:58 PM
at formatValue (internal/util/inspect.js:493:31)
9:58 PM
at inspect (internal/util/inspect.js:191:10)
9:58 PM
at Object.formatWithOptions (util.js:84:12)
9:58 PM
at Console.(anonymous function) (console.js:191:15)
9:58 PM
at Console.log (console.js:202:31)
9:58 PM
Jump Toat /app/server.js:107:39
9:58 PM
at /rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/helpers/promiseOrCallback.js:16:11
9:58 PM
at /rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:4860:21
9:58 PM
at _done (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:3120:16)
9:58 PM
at fn (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:3135:18)
忽然笑
四季花海
相关分类