我想知道是否可以設定從元素陣列中選擇的默認值.. 例如:
const userSchema = new mongoose.Schema({
username: {
type: 'String',
require: [ true, "Please choose a username!" ],
trim: true,
unique: true
},
...etc ...
.....
avatar: {
type: 'String',
default: '[FOR EXAMPLE HERE, TO TAKE RANDOMLY A VALUE FROM A LIST]'
},
更準確地說,我想為那些沒有為他們的頭像選擇照片的用戶設計幾個頭像標志。所以我想要一個帶有圖片網址的串列。所以..可以從串列中隨機選擇一個默認值嗎?
uj5u.com熱心網友回復:
您可以創建一個函式從串列中隨機分配
avatar: {
type: 'String',
default: function() {
return avatarArrayList[Math.floor(Math.random()*avatarArrayList.length)];
}
},
有關更多資訊,請查看https://mongoosejs.com/docs/defaults.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399220.html
