Vapor MongoDB Provider Error - swift

I am trying to run a Vapor app on my local machine and have MongoDb installed and running.
I have this as my mongo.json: {
"user": "test",
"password": "password",
"database": "reading_journal",
"host": "127.0.0.1",
"port": 2701
}
which is correct in terms of the info for the local DB.
My main.swift:
import Vapor
import FluentMongo
import VaporMongo
let drop = Droplet(providers: [VaporMongo.Provider.self])
drop.get { req in
let lang = req.headers["Accept-Language"]?.string ?? "en"
return try drop.view.make("welcome", [
"message": Node.string(drop.localization[lang, "welcome", "title"])
])
}
drop.resource("users", UserController())
drop.resource("posts", PostController())
drop.run()
Yet in the log I get: "Could not initialize provider Provider: Socket failed with code 61 ("No data available") [connectFailed] "Unknown error"
Is there some other initialization that needs to be done? This is a brand new MongoDB DB.
Any help would be greatly appreciated!

In my case, I had to add "host": "0.0.0.0" in the mongo.json

That error usually happens if MongoDB is not running on the correct port. Make sure whatever you have in your mongo.json file matches what port MongoDB is running on.

Related

MongoRuntimeError Unable to parse with URL

I am trying to connect to a mongo db using the nodejs mongo driver and I'm doing this in a cypress project. I get the error in the title. Below is the simplified version of my code.
import {MongoClient} from 'mongodb';
export class SomeRepository {
static insertSomething(): void {
// Error in the line below: MongoRuntimeError Unable to parse localhost:27017 with URL
const client = new MongoClient('mongodb://localhost:27017');
}
}
Mongodb is running because I can connect from the terminal. Also tried replacing localhost with 127.0.0.1 and adding the authSource parameter to the connection string.
The reason I'm mentioning cypress is because in a simple node project that only connects to mongodb everything works as expected. Package.json below
{
"name": "e2e",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"cypress": "10.8.0",
"cypress-wait-until": "1.7.2",
"headers-utils": "3.0.2",
"mongodb": "4.10.0",
"otplib": "12.0.1",
"pg": "8.7.3",
"pg-native": "3.0.1",
"typescript": "4.9.3"
}
}
The error is in the way you are passing the url, it is necessary that you follow a pattern, in mongodb to connect you need to have this pattern that I will pass below:
Format:
mongodb://<user>:<password>#<host>
Format with filled values:
mongodb://root:mypassword#localhost:27017/
The reason it’s not working is because you’re calling a NodeJS library in a cypress test. Cypress tests run inside a browser and cannot run nodejs libraries.
If you wanted to execute nodejs code in cypress you must create a cypress task https://docs.cypress.io/api/commands/task#Syntax
// cypress.config.js
import { SomeRepository } from ‘./file/somewhere’
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on(‘task’, {
insertSomething() {
return SomeRepository.insertSomething();
}
}
}
}
})
// to call in a cypress test
it(‘test’, function () {
cy.task(‘insertSomething’).then(value => /* do something */);
}
});

How can I start nest app with Postgress DB on Win 10?

I'm front dev and I need to test locally my front app with backend (nest js) and postgresql DB. Who can write me the right way How to run and connect to DB ? I get some errors on app start. I work on win 10 and there is my steps for start this app.
install postgresql
npm install for my nest js app
run pgAdmin4 and create DB for my app
npm start
There is my ormconfig
module.exports = {
"type": "postgres",
"host": process.env.POSTGRES_HOST || "localhost",
"port": process.env.POSTGRES_PORT || 5432,
"username": process.env.POSTGRES_USER || "", //<- Here I try to set all possible username
"password": process.env.POSTGRES_PASSWORD || "", //<- Here I try to set all possible password
"database": process.env.POSTGRES_DB || "my_database",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true,
"logging": true
}
There is error that I encountered
error
Also on other computer I try to do this and I get error like
[Nest] ERROR [TypeOrmModule] Unable to connect to the database.
FATAL: password authentication failed for user "postgres" (postgresql 14 with pgAdmin 4)
In your typeormconfig.ts , you should write this:
export class PostgresTypeormConfiguration implements TypeOrmOptionsFactory
{
createTypeOrmOptions(connectionName?: string): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
const TypeOrmOptions:TypeOrmModuleOptions=
{
type: "postgres",
host: process.env.POSTGRES_HOST ,
port: process.env.POSTGRES_PORT ,
username: process.env.POSTGRES_USER ,
password: process.env.POSTGRES_PASSWORD ,
database: process.env.POSTGRES_DB,
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
logging: true
}
return TypeOrmOptions
}
}
and you should define this in your module like this:
#Module({
imports:[TypeOrmModule.forRootAsync({useClass:PostgresTypeormConfiguration})]
})
note: if you still got an error , you wrote one of the config option wrong in your .env file or you did not define .env file in your configModule

How to fix: 'TypeError: Cannot initialize connector undefined:' while use loopback-connector-firestore

I tried several answers from stackoverflow but cannot solve the problem, I want to connect the firestore to loopback using this package: loopback-connector-firestore (https://www.npmjs.com/package/loopback-connector-firestore), after create the datasource using lb datasource command and start the system, error below will show out:
TypeError: Cannot initialize connector undefined: Cannot read property 'replace' of undefined
The loopback already connected to other datasource. How can I add firestore into it?
This is the datasources.json file:
{
-----other db datasources here-----
"Firestore": {
"name": "Firestore",
"projectId": "project id",
"clientEmail": "client email",
"privateKey": "key here",
"databaseName": "name here",
"connector": "loopback-connector-firestore"
}
}
In server.js file:
var ds = loopback.createDataSource({
connector: require('loopback-connector-firestore'),
provider: 'Firestore'
});
var storage = ds.createModel('storage');
app.model(storage);
The environment settings:
* Kubuntu 18.04
* nodejs v10.16
* npm v6.9
* loopback v3
I think for now the some of the connectors aren't supported by Loopback, and 'Loopback-firebase-connector' is one of them.
you may need to check the following:
Reference: https://loopback.io/doc/en/lb3/Community-connectors.html

Loopback 4 test configurations are not picked up

I followed Loopback4 datasources documentation and placed sample.datasource.json and sample.test.datasource.json files under src/datasources. Whenever I run npm run test my repository is injected with original datasource but not the test datasource.
My datasource configuration file sample.datasource.json is
{
"name": "sample",
"connector": "postgresql",
"url": "postgres://postgres:user#localhost:5432/somedb",
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "****",
"database": "somedb"
}
and my test configuration file sample.test.datasource.json is
{
"name": "sample",
"connector": "memory",
"localStorage": "sample-test",
"debug": true
}
Here is my repository file sample.repository.ts placed under src/repositories.
export class SampleRepository extends DefaultCrudRepository<SomeModel,string> {
constructor(
#inject('datasources.sample') dataSource: SomeDataSource,
) {
super(SomeModel, dataSource);
}
}
I exported NODE_ENV to "test" and tried to run npm run test.
Hello from the LoopBack team :)
LoopBack 4 does not support environment specific configuration yet. We are discussing this feature in the GitHub issue #1464, there is also a discussion about modifying datasource configuration specifically for tests - see issue #1396.
There are workaround possible, see the following resources for inspiration:
https://loopback.io/doc/en/lb4/Deploying-to-IBM-Cloud.html#updating-application
https://itnext.io/loopback-4-database-configuration-8f085399268

Centrifuge not using MongoDB?

I just installed centrifuge (https://centrifuge.readthedocs.org/en/latest/) and created a configuration.json file and placed it in /var/www/ folder.
When I try to run centrifuge centrifuge config = /var/www/configuration.json, the server starts. However when I go to the default path http://localhost:8000 in the admin panel it keeps saying DataStructure used as SQLite.
Here's my configuration.json file
{
"password": "admin",
"cookie_secret": "secret",
"api_secret": "secret",
"structure": {
"storage": "centrifuge.structure.mongodb",
"settings": {
"host": "localhost",
"port": 27017,
"name": "centrifuge",
"pool_size": 10
}
},
state: null
}
I checked and the MongoDB server is running on port 27017.
It seems you are starting Centrifuge using incorrect command line arguments. Try copy and paste into your terminal:
centrifuge --config=/var/www/configuration.json