类型错误:架构配置无效:"True"不是路径"时间戳"处的有效类型?



首先,让我向你保证,我已经搜索了所有关于这个错误的地方,但似乎不存在任何接近这种错误的东西。我在猫鼬计划中使用了timestamps: true,。这应该在我的架构中自动生成"created_at"和"updated_at"。但是这一行抛出了这个错误:TypeError: Invalid schema configuration: 'True' is not a valid type at path 'timestamps'.然后我尝试写timestamps: {Type:Date, required:true}.在这种情况下,没有运行时错误,但是当我发布请求时,它基本上会抛出验证错误,因为时间戳是必需的,但正文中没有提供。这显然意味着关键字timestamps没有正常工作,因为它应该是自动生成的。

相信我,当我说我试图尽可能地自己找到问题时,因为在这里发布一个新问题的想法有点让我做噩梦:3 爱堆栈溢出虽然<3

不要在架构本身中传递它,{timestamps: true}作为构造函数中的选项传递。

const exampleSchema = new Schema(
{
name: {
type: String,
},
},
{
timestamps: true,
}
);

对于猫鼬 5.x 时间戳是使用您创建的MongoDB集群自动创建的。 删除时间戳,如果确实需要时间戳,请使用"日期" 或者您可以使用

$ npm install --save mongoose mongoose-timestamp 

const mongoose = require('mongoose'), timestamps = require('mongoose-timestamp')

相关内容

最新更新