Javascript “new mongoose.Schema”每次都使用相同的默认值`

Javascript “new mongoose.Schema”每次都使用相同的默认值`,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我无法将uuid与newmongoose.Schema一起使用。我使用它为设备生成唯一密钥,并使用Node.js将其保存到MongoDb。问题是它每次都使用相同的UUID 这就是模型: const mongoose = require('mongoose'); const uuid = require('uuid/v4'); const DeviceSchema = new mongoose.Schema({ deviceNumberHash: { type: Stri

我无法将
uuid
newmongoose.Schema
一起使用。我使用它为设备生成唯一密钥,并使用Node.js将其保存到MongoDb。问题是它每次都使用相同的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中保存的内容:


知道怎么回事吗?

您正在调用
uuid
,并将其返回值作为默认值传入

相反,传入函数(不要在其后面加
()
):

const DeviceSchema=new mongoose.Schema({
DeviceNumber哈希:{
类型:字符串,
必填项:true
},
接收键:{
类型:字符串,
默认值:uuid//
const DeviceSchema = new mongoose.Schema({
    deviceNumberHash: {
        type: String,
        required: true
    },
    receivingKey: {
        type: String,
        default: uuid // <========== No ()
    }...
});