-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use inside use doesn't work anymore #30
Comments
what is in ./index.js? |
function antoherPlugin(fastify, opts, next) { next() } |
The problem are nested register calls. Don’t use the callbacks. |
Yeah for independent plugins it should work. But if the internal plugin
uses some decoration provided by the external one, how could I describe
that dependency?
Maybe `after`?
Il sab 14 ott 2017, 20:48 Matteo Collina <notifications@github.com> ha
scritto:
… The problem are nested register calls. Don’t use the callbacks.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABAVrY6AF0sU6qWmHP4ahx7ODTitEZNeks5ssQHjgaJpZM4P5YWS>
.
|
It’s already handled, you don’t have to do anything. Use another register in the main plugin. |
I don't understand. My situation is like this (in fastify): function plugin1 (fastify, opts, next) {
setTimeout(function () { // silumate some async
fastify.decorate('foo', 4) // decorating with the result of the async operation
}, 100)
}
function plugin2 (fastify, opts, next) {
console.log(fastify.foo) // using the previous set value
} How can I register this doesn't work anymore EDIT: How can I write the dependency betweet function plugin2 (fastify, opts, next) {
console.log(fastify.mongo) // use fastify-mongodb
} |
Just don’t use series. It will work as expected, it will be present in any subsequent plugin that you use. The callback in register is kind of useless. |
I'm very confused regarding whatever it is that has changed. I would imagine developers would want to know that something such as a DB connection is broken before the call to My own code is currently split into a collection of plugins, each of which is registered, and then a collection of routes. This now breaks with the "already loaded" error. Can the expected use of |
@wooliet there is no need to declare the routes after loading all the dependent plugins. This is already done for you, just call If you need to wait for whatever reason, wrap it in a plugin. The previous order was non-deterministic, and it caused some weird bugs. This is very deterministic. Really, you should not use the |
@mcollina I'm sorry to bother you guys with this, and I'll be glad to move this to the fastify repo if you prefer. But I think there's a communication breakdown somewhere. Copied below is code that is representative of what used to work (fastify 0.29.2) but now (0.30.2) results in:
I am interested in what an updated version should look like. const async = require('async');
const formbody = require('fastify-formbody');
const fastify = require('fastify')();
async.waterfall([
next => plugins(next),
next => routes(next)
], (err) => {
if(err) process.exit(1);
fastify.listen('9090', '0.0.0.0', err => console.log('running'));
})
function plugins(done) {
fastify.register(formbody, {}, (err) => {
done(err);
});
}
function routes(done) {
fastify.get('/ping', (req, resp) => {
resp.code(204).send();
});
done();
} |
@mcollina we should remove the callback definitely. Anyway, fastify.register(require('fastify-mongodb'), { … })
fastify.register(function (f, o, n) {
var myColl = f.mongo.db.collection('my-collection') // f.mongo is defined!
}) works for me. |
@allevo at what point in that flow do you call |
Maybe my hangup is that I don't want to open up the port for incoming requests until I know everything has successfully initialized. But if that's not possible anymore then ok, no biggie. |
'use strict'
fastify.register(require('fastify-mongodb'), { … }) // set up the mongo connection
fastify.register(function (f, o, n) {
// please don't drop you database in production!!!!
return f.mongo.db.dropDatabase() // the connection is made
})
fastify.register(function setRoutes(f, o, n) {
var myColl = f.mongo.db.collection('my-collection') // the connection is made
f.get('/', function (req, reply) {
reply.send(myColl.find({}).toArray())
})
})
fastify.listen(function (err) {
// server up if err is falsy!
}) I'll close this conversation because the issue is "resolved": the If you are other questions/issues please open another one (in |
Using fastify, this code works correctly in
0.28
(avvio
version 2):In
0.30.2
(avvio
version 3) the second error has this message:Impossible to load "pluginOk" plugin because the parent "" was already loaded
This behaviour is introduced by #29 on avvio version 3.
How can I rewrite the previous code in new avvio version?
The text was updated successfully, but these errors were encountered: