简体   繁体   English

Meteor 尝试在服务器端插入文件时文件集合显示为空?

[英]Meteor Files Collection showing up empty when trying to insert files on server-side?

I'm using the Meteor Files package (from https://github.com/VeliovGroup/Meteor-Files ) in my Meteor project.我在我的 Meteor 项目中使用 Meteor 文件 package(来自https://github.com/VeliovGroup/Meteor-Files )。 I want to set up a files collection but I'm having issues populating it from the server-side.我想设置一个文件集合,但在从服务器端填充它时遇到问题。

Here is my relevant code inside /server/main.js :这是我在/server/main.js中的相关代码:

import { Sources } from "/imports/db/sourcesCollection";


Meteor.startup(() => {
  // code to run on server at startup

  Sources.addFile("/images/bigLogo.png", {
    fileName: "bigLogo.png",
    type: "image/png",
    fileId: "abc123AwesomeId",
    meta: {},
  })
...

And here is sourcesCollection.js :这是sourcesCollection.js

import { FilesCollection } from "meteor/ostrio:files";

const Sources = new FilesCollection({ collectionName: "Sources" });

export { Sources };

bigLogo.png is an image png file inside the public/images directory in my project, and I've used it successfully in other files. bigLogo.png是我项目中public/images目录下的一个 png 图像文件,我已经在其他文件中成功地使用了它。 As a sanity check, to test if this was a file-path issue, I've also tried moving my image to the same directory as my server/main.js file and changing the image path to ./bigLogo.png , but to no avail.作为完整性检查,为了测试这是否是文件路径问题,我还尝试将我的图像移动到与我的server/main.js文件相同的目录并将图像路径更改为./bigLogo.png ,但是徒劳无功。

When my server starts up I get no error messages so it seems like addFile() is working, but when I check my collection on the server-side through the meteor shell, it always shows up as an empty collection.当我的服务器启动时,我没有收到任何错误消息,所以看起来addFile()正在工作,但是当我通过 meteor shell 在服务器端检查我的集合时,它总是显示为一个空集合。 Here is what I'm doing to check it:这是我正在做的检查:

First, I run meteor shell , then import { Sources } from "imports/db/sourcesCollection.js" and finally Sources.find({}).fetch() which returns an empty array [] .首先,我运行meteor shell ,然后import { Sources } from "imports/db/sourcesCollection.js" ,最后返回一个空数组[]的 Sources.find Sources.find({}).fetch()

To summarize, my question is: if my code is running successfully when my server starts up, why is my collection showing up as empty in my meteor shell?总而言之,我的问题是:如果我的代码在我的服务器启动时成功运行,为什么我的集合在我的 meteor shell 中显示为空?

Edit: Running the .addFile() statement in the console returned a FilesCollection object, and the collection is still empty upon checking Sources.find({}).fetch() .编辑:在控制台中运行.addFile()语句返回一个 FilesCollection object,并且在检查Sources.find({}).fetch()时该集合仍然为空。

Edit 2: I added a callback function to display the error from addFile().编辑 2:我添加了一个回调 function 来显示来自 addFile() 的错误。 I'm getting an ENOENT error referring to the file path.我收到有关文件路径的 ENOENT 错误。 The image is most definitely there and the path is correct (I've tried both in the same folder and in the public folder), so I don't know why this is happening.该图像绝对存在并且路径正确(我已经在同一文件夹和公共文件夹中尝试过),所以我不知道为什么会这样。 I've tried resetting the project and even re-installing Meteor but that didn't fix it either.我试过重置项目,甚至重新安装 Meteor,但这也没有解决问题。

If you ever run into this issue, here is what fixed it for me:如果您遇到过这个问题,以下是为我解决的问题:

Meteor: Error: ENOENT when trying to access an image using node-gd Meteor:错误:尝试使用 node-gd 访问图像时出现 ENOENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM