Meteor 和 React Native - 文件集合访问

发布于 2025-01-09 17:44:05 字数 1532 浏览 0 评论 0原文

我不久前在 FilesCollection Github Repo 上发布了这个问题(https://github.com/veliovgroup/Meteor -Files),但我希望能在这里联系到任何熟悉 Meteor 和 React Native 的人。

我的问题是,我不确定如何将 FilesCollection 与 React Native (RN) 和 Meteor 一起使用。

我使用官方 Meteor 指南来设置 RN 应用程序: https://guide.meteor.com /react-native.html

所以我可以访问 @meteorrn/core 但现在我怎么可能访问 FileCollection。

通常你会这样做(在非 RN Web 应用程序上):


import { FilesCollection } from "meteor/ostrio:files";
export const ImageDB = new FilesCollection(chosenSettings);

其中 chosenSettings 是一些设置可能是例如

const chosenSettings = {
  collectionName: "Images",
  allowClientCode: false,
  onBeforeUpload(file) {
    if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
      return true;
    }
    return "Please upload image, with size equal or less than 10MB";
  },
  storagePath: `${process.env.PWD}/images`,
};

但是,使用 RN 无法访问 FilesCollection来自 meteor/ostrio:files 因为我在这里没有任何流星关系

对于其他集合,我可以使用来自 @meteorrn/coreMongo.Collection >,但这对于 FilesCollection 来说是不可能的:

const collection = new Mongo.Collection("myCollection");

有什么建议吗?

i posted this question some time ago at the FilesCollection Github Repo (https://github.com/veliovgroup/Meteor-Files), but I'll hope to reach out here anyone who is familiar with Meteor and React Native.

My problem is, that I'm not sure how to use a FilesCollection with React Native (RN) and Meteor.

I used the official Meteor guide to set up a RN app: https://guide.meteor.com/react-native.html

So i have access to @meteorrn/core but how is it now possible for me to access a FileCollection.

Usually you would do this with (on a non-RN-web app):


import { FilesCollection } from "meteor/ostrio:files";
export const ImageDB = new FilesCollection(chosenSettings);

where chosenSettings are some settings might be e.g.

const chosenSettings = {
  collectionName: "Images",
  allowClientCode: false,
  onBeforeUpload(file) {
    if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
      return true;
    }
    return "Please upload image, with size equal or less than 10MB";
  },
  storagePath: `${process.env.PWD}/images`,
};

However, with RN it is not possible to access FilesCollection from meteor/ostrio:files as I don't have any meteor relation here

With other collections I can use Mongo.Collection from @meteorrn/core, however this is not possible with FilesCollection:

const collection = new Mongo.Collection("myCollection");

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2025-01-16 17:44:05

不存在“开箱即用”的解决方案,因为 RN 客户端上没有可用的 FilesCollection。但是,您可以使用 Meteor 方法创建解决方法策略。

  1. 为 FilesCollection 的基础集合创建一个发布:

服务器

Meteor.publish('imagesForRN', function () {
  return ImageDB.collection.find({}) // note the Mongo.collection is accessible here
})
  1. 创建一个解析链接的方法:

服务器

Meteor.methods({
  resolveRNLinks ({ ids }) {
    return ImageDB.collection
      .find({ _id: { $in: ids}})
      .map(linkImage);
  }
})

const linkImage = function (fileRef) {
  const url = Images.link(fileRef, 'original', Meteor.absoluteUrl())
  return { _id: fileRef._id, url }
}
  1. 获取订阅文档的 URL

这假设您已订阅了图像集合,如图所示在指南中。

客户端

import Meteor from '@meteorrn/core'

const MyComponent = props => {
  const { loading, myTodoTasks } = this.props;

  useEffect(() => {
    const ids = myTodoTasks.map(doc => doc._id)
    // note, you should filter those ids, which you have already
    // resolved so you only call for the new ones
    Meteor.call('resolveRNLinks', { ids }, (err, urls) => {
      // store urls in state or ref, depending on your usecase
    })
  })
}

资源

There is no "out-of-the-box" solution, as there is no FilesCollection on the RN client available. However, you could create a workaround strategy using a Meteor method.

  1. Create a publication for the FilesCollection's underlying collection:

server

Meteor.publish('imagesForRN', function () {
  return ImageDB.collection.find({}) // note the Mongo.collection is accessible here
})
  1. Create a Method to resolve links:

server

Meteor.methods({
  resolveRNLinks ({ ids }) {
    return ImageDB.collection
      .find({ _id: { $in: ids}})
      .map(linkImage);
  }
})

const linkImage = function (fileRef) {
  const url = Images.link(fileRef, 'original', Meteor.absoluteUrl())
  return { _id: fileRef._id, url }
}
  1. Get the URLS of the subscribed docs

This assumes you have subscribed to the Image Collection as shown in the guide.

client

import Meteor from '@meteorrn/core'

const MyComponent = props => {
  const { loading, myTodoTasks } = this.props;

  useEffect(() => {
    const ids = myTodoTasks.map(doc => doc._id)
    // note, you should filter those ids, which you have already
    // resolved so you only call for the new ones
    Meteor.call('resolveRNLinks', { ids }, (err, urls) => {
      // store urls in state or ref, depending on your usecase
    })
  })
}

Resources

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文