![](/img/trans.png)
[英]What is the difference between mongoose.Schema() and new mongoose.Schema()?
[英]The same default value gets used every time by `new mongoose.Schema`
我有問題使用new mongoose.Schema
uuid
。 我使用它為設備生成唯一鍵,並使用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()
}...
});
知道什么是錯的嗎?
您正在調用 uuid
並將其返回值作為默認值使用。
相反,傳入函數 (通過不在其后put ()
):
const DeviceSchema = new mongoose.Schema({
deviceNumberHash: {
type: String,
required: true
},
receivingKey: {
type: String,
default: uuid // <========== No ()
}...
});
默認值可以是每個文檔的一個函數(例如,使用default: Date.now
為日期字段提供默認值)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.