我在new mongoose.Schema中使用new mongoose.Schema时遇到了问题。我使用它为设备生成唯一的密钥,并使用Node.js将其保存到Node.js中。问题是它每次都使用相同的UUID。
这就是模式:
const mongoose = require('mongoose');
const uuid = require('uuid/v4');
const DeviceSchema = new mongoose.Schema({
deviceNumberHash: {
type: String,
required: true
},
receivingKey: {
type: String,
default: uuid()
}...
});这就是在MongoDb中保存的内容:

你知道怎么回事吗?
发布于 2019-04-13 10:15:57
您正在调用 uuid并将其返回值作为默认使用。
相反,传入函数(通过不将()放在后面):
const DeviceSchema = new mongoose.Schema({
deviceNumberHash: {
type: String,
required: true
},
receivingKey: {
type: String,
default: uuid // <========== No ()
}...
});默认值可以是函数按照医生的说法 (例如,使用default: Date.now为日期字段提供默认值)。
https://stackoverflow.com/questions/55664330
复制相似问题