Iād console.log for createModel and see what it isā¦
I donāt know if you use and .npmrc and what configurations you have in it but the easiest way to go forward would be to locally install your npm package.
Say in your root directory create a localNPM folder. Add your package folder there and install it with npm i "file:localNPM/packagename." You can name it something like ā@xta/modelā to recognize it beside the official NPMs from the public repos or @xta-local.
If all ok you can publish it somewhere as a private package.
Everything is okay there, I get the structure for the model when I do a console.log. The only issue is the save function which also triggers an event for redis-oplog:
W20230717-17:12:15.693(2)? (STDERR) (node:2271) UnhandledPromiseRejectionWarning: Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20230717-17:12:15.693(2)? (STDERR) at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor.js:1320:11)
W20230717-17:12:15.693(2)? (STDERR) at Meteor.EnvironmentVariable.EVp.get (packages/meteor.js:1345:10)
W20230717-17:12:15.694(2)? (STDERR) at packages/cultofcoders:redis-oplog/lib/mongo/lib/getMutationConfig.js:17:39
W20230717-17:12:15.694(2)? (STDERR) at hook.insert (packages/cultofcoders:redis-oplog/lib/mongo/Mutator.js:53:24)
W20230717-17:12:15.694(2)? (STDERR) at hook.insert (packages/cultofcoders:redis-oplog/lib/mongo/extendMongoCollection.js:29:35)
W20230717-17:12:15.694(2)? (STDERR) at hook.Mongo.Collection.<computed> [as insertAsync] (packages/mongo/collection.js:1003:46)
What I donāt understand is why it works if I copy the createModel function into a project file and fails if I import it via a npm package? Does Meteor thread such imports differently?
Ok, an opinion but I am not 100% on it. Ok ā¦ maybe 20% max :)))
"/imports/others/model.js" is both client and server because is not in a āserverā folder.
If you call createModel inside a Method which you call from the client, it may run with the stub.
When you install an NPM and you import from NPM you import server side only and you run everything on the server, thus the need for binding to the environment.
I donāt know your code structure but perhaps try to run everything async or bind to the environment.
const bound = Meteor.bindEnvironment(callback => callback())
bound(() => { /* put all your relevant code here. */ })
Yees, it is the right direction. Iāve checked the this context within the createModel function. If I import it via my project files, the function is bound to the Meteor environment. If I import it via npm, the this context is undefined.
This fixed the issue, but I have to check if I can do the bindEnvironment within the npm package so it gets binded automatically:
import {createModel} from 'model';
const createModelBinded = createModel.bind(this);
I already called only the async functions to avoid fibers, but it seems that redis-oplog still depends on it, so I hope they will fix it soon after Meteor 3 has been released.
Mhh seems that this wasnāt the solution. I really donāt understand why it works if I add the content straight within a project file or within an internal meteor package, but fails if I import the code via npm. It seems that there is something different with the bindings, but I wasnāt able to bind it corretly with .bind or Meteor.bindEnvironment.
The function looks like this:
function createModel({name, collection, schema, events, helpers, relations}) {
function Model(params) {
[...]
this.save = async function() {
[...]
await Model.collection.insertAsync(cleanedValues); // Native async insert function of Meteor
}
}
return Model;
}
export {createModal};