There was no result because of the internal server error. You need to figure out and solve the internal server error first. Check your server console or logs for more details
Do add to @rjdavid your code will probably never reach the second condition searching in Collection2 because, if I am not wrong, collection.find always returns a cursor (and if you do fetch() you get an array/empty array). Collection.find() is always true. You can do findOne() which returns undefined if no document exists as per your query or Object if a document is found.
You can also return Collection.find({ condition }).fetch()?.[0] if you want this to be a correct true / false condition.
But I’m still getting "Internal server error [500]" on the client side.
On the terminal, I’m seeing an error massage saying Exception from sub attachments id WCZyvMDxtDtHtkxwW Error: Publish function returned an array of non-Cursors
In your last example, you key is _id not uuid.
Just some comments about methods.
You can use something called an optimistic UI. In this case, your method has to be defined on both Client and Server so that the Client performs a simulation of the result for a faster UI response.
The most secure way to run methods is to define them on the server and call them from the client.
To define Server only, you can just put a methods.js file inside a “server” folder. All folders named as “server” are treated as server only files, everywhere in your project tree.
If however you define it outside a “server” folder, you can include the entire method function within a conditions such as:
Many of us use a library called ValidatedMethod in which case your method looks like:
import { Meteor } from 'meteor/meteor'
import { ValidatedMethod } from 'meteor/mdg:validated-method'
import SimpleSchema from 'simpl-schema'
new ValidatedMethod({
name: 'file.get',
validate: new SimpleSchema({
_id: String, /* { type: String, optional: true } if _id is not a mandatory argument. */
}).validator(),
run ({ _id }) {
if (Meteor.isServer) {
console.log(_id)
if (!this.userId) { throw new Meteor.Error('not-authorized') } // check a user is logged in if this is your case.
let file = Collection1.findOne(uuid) // this is equivalent of Collection1.findOne({ _id: uuid }). If you only query for the _id, you can just skip the Object format.
if (file) {
return file
}
file = Collection2.findOne(uuid) // this will return undefined if no document with this id is found.
return file
}
}
})
I thought the return has to be like
return Collection1.findOne({_id: uuid}).fetch();
But again I’mgetting TypeError: Cannot read properties of undefined (reading 'fetch') message, so I don’t need fetch() all the time Or I do need fetch() if it’s Collection1.find() ?
Hi @miki11 I read this entire thread and I believe no one has addressed a couple of basic questions you’ve made:
The [ ] in the Meteor.call definition do not mean “array” but rather “optional”. This is a very common notation in API documentation. You can pass as many arguments as you want to a method, and these args can be of any type. You can as well pass a callback as the last parameter to your Method.call call, to do something with the result of that call. Nor the arguments nor the callback need to be inside an array (and in particular, if you put the callback inside an array it may get interpreted as an argument instead I believe)
@paulishca already answered the basics on find & fetch but to me it seems you have a deeper missunderstanding on what is going on (no disrespect intended).
A method (anything defined inside Metheor.methods) is basically an API. So what it returns its completely up to you. It doesn’t have to return anything in particular (as a custom defined API).
A publication (anything defined with Meteor.publish) on the other side, has to return a cursor. That means, every publication definition must end withreturn Collection.find(searchObject, optionsObject).
These are very basic Meteor concepts so yes, besides more practice I’d say you need to take a deeper, slower dive into the docs in order to better understand the concepts. Otherwise you may end up wasting a lot of time debugging. Don’t know if you’ve done it but following the meteor tutorial for the ToDo App may help clarify all the concepts and the usual way of using things