簡體   English   中英

使用Mongoose將GridFS-Stream文件與架構關聯

[英]associating a GridFS-Stream file to a Schema with Mongoose

我正在使用Mongoose,Express和GridFS-Stream為我的應用程序編寫API。 我為用戶將創建的文章提供了一個架構:

var articleSchema = mongoose.Schema({
    title:String,
    author:String,
    type: String,
    images: {type: Schema.Types.ObjectId, ref: "fs.files"},
    datePublished: { type: Date, default: Date.now },
    content: String
})
var Article = mongoose.model("article", articleSchema, "articles");

和我的grid-fs設置為用戶上傳圖像的時間:

api.post('/file', fileUpload.single("image"), function(req, res) {
var path = req.file.path;
var gridWriteStream = gfs.createWriteStream(path)
    .on('close',function(){
        //remove file on close of mongo connection
        setTimeout(function(){
            fs.unlink(req.file.path);
        },1000);
    })
var readStream = fs.createReadStream(path)
    .on('end',function(){
        res.status(200).json({"id":readStream.id});
        console.log(readStream);
    })
    .on('error',function(){
        res.status(500).send("Something went wrong. :(");
    })
    .pipe(gridWriteStream)

});

現在設置為用戶選擇圖像時,它會自動通過gridfs-stream上載它,將其放在temp文件夾中,然后在上載到mongo服務器時將其刪除,然后在控制台中返回ObjectId是什么。 好了,這一切都找到了,但我們需要將此ID與articleSchema關聯,因此,當我們在應用程序中調用該文章時,它將顯示關聯的圖像。

當用戶點擊“提交”時,我們在創建/更新文章時:

createArticle(event) {
event.preventDefault();
var article = {
  type: this.refs.type.getValue(),
  author: this.refs.author.getValue(),
  title: this.refs.title.getValue(),
  content: this.refs.pm.getContent('html')
};
var image = {
  images: this.refs.imageUpload.state.imageString
};
var id = {_id: this.refs.id.getValue()};
var payload = _.merge(id, article, image);
var newPayload = _.merge(article, image)
if(this.props.params.id){
  superagent.put("http://"+this.context.config.API_SERVER+"/api/v1.0/article/").send(payload).end((err, res) => {
      err ? console.log(err) : console.log(res);
  });
} else {
  superagent.post("http://"+this.context.config.API_SERVER+"/api/v1.0/article").send(newPayload).end((err, res) => {
    err ? console.log(err) : console.log(res);
    this.replaceState(this.getInitialState())
    this.refs.articleForm.reset();
  });
}

},

因此,我需要做的就是調用ID,即當用戶在創建文章時點擊“提交”時我剛剛上傳到架構的圖像部分的圖像。 我嘗試在提交時執行讀取流,但同樣,問題是我無法獲取ID或文件名來關聯它。

它們被存儲在mongo數據庫中,它創建fs.files和fs.chunks,但是對於我一生,我無法弄清楚如何獲取該數據並將其附加到架構,甚至無法獲取數據,而不知道ObjectId。

那么,如何從fs.files或fs.chunks中調出對象ID,以將其附加到架構? 在模式中,如何引用fs.files或塊? 這樣就知道了objectid與什么關聯?

我可以提供更多數據,如果我有什么含糊的地方,我有這樣做的討厭習慣。 抱歉。

因此,我最終解決了我的問題,可能不是最好的解決方案,但是直到獲得更好的解決方案,它才起作用。

在API中已更改

res.status(200).json({"id":readStream.id});

res.status(200).send(readStream.id);

然后,在我的組件中,將狀態設置為response.body,這將設置上載圖像ID的狀態。 因此,在主視圖中,我引用了圖像上傳組件,並將視圖的圖像狀態設置為組件的id狀態,然后中提琴,我現在在數據庫中擁有與新創建的文章相關聯的id。

然后我遇到的問題是,它不知道要參考什么。 因此我將API URL附加到id上,它的行為就像是在引用URL img一樣,並正確呈現了圖像。

再說一次,這可能不是解決此問題的最佳方法,事實上,我敢肯定這不是最好的方法,但是直到現在我可以正確地引用數據庫或創建一個僅存儲新組件的方法,這才是可行的服務器上的所有圖像並以這種方式引用它們,就像wordpress一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM