我將影像存盤在緩沖區中并在新聞中參考。當我填充影像時,我需要它們作為 Base64 URL。是否可行
const mongoose = require('mongoose')
const Schema = mongoose.Schema({
title: String,
description: String,
images: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'File',
get: (image) => {
return {
title: image.title,
url: `data:${image.mimeType};base64,${Buffer.from(image.data.data).toString('Base64')}`
}
}
}],
content: String,
status: String,
date: {
type: Date,
default: () => new Date()
}
}, {
collection: 'news'
})
module.exports = mongoose.models.News || mongoose.model('News', Schema)
uj5u.com熱心網友回復:
不在架構宣告中。每個模式都映射到一個 MongoDB 集合,并定義您應該在此處查找有關有效型別的檔案的形狀(在此處查看有關 SchemaTypes 的更多資訊)
我可以想到兩種解決方案。首先是將File影像保存為 Buffer 而不是 base64。
第二種解決方案是使用地圖功能在填充后轉換您的影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/326703.html
上一篇:在node.js應用程式中應該在哪里寫res.send()
下一篇:Next.js Typescript mongoose使用`letcached=global.mongoose`時出錯
