Reputation: 2979
I'm trying to follow Getting started with LoopBack but instead of connecting to MySql instance I'm trying to connect to MongoDB instance running on localhost (default port 27017).
While following the steps I'm executing node .
as given at Connect your API to a data source This is giving me following error and then node is stopping.
D:\TestLoopBack\node_modules\loopback-connector-mongodb\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; }); ^ ValidationError: The `CoffeeShop` instance is not valid.
Details:
Name
can't be blank (value: undefined).,ValidationError: TheCoffeeShop
instance is not valid. Details:Name
can't be blank (value: undefined).,ValidationError: TheCoffeeShop
instance is not valid. Details:Name
can't be blank (value: undefined).
Can someone help me understand what is this error and how can I resolve it?
My datasources.json file has:
{ "db": { "name": "db", "connector": "memory" }, "mongoDs": { "name": "mongoDs", "connector": "mongodb", "host": "localhost", "port": 27017, "database": "TestLoopBackDB" } }
I've not set any authentication on MongoDB so no username / password specified.
Upvotes: 0
Views: 931
Reputation: 2979
It was a silly mistake. :(
While defining the model I had typed Name
as the property. And I just copy-pasted automigrate
code from the Connect your API to a data source which had the property name
in lower case. After making case same at both places it worked without any issue :)
Upvotes: 0
Reputation: 307
Befor using any collection from MongoDB if you are using ready made collection of mongoDB then first Discover the model and then use in your loopback application. to discover any model use the following script in server/bin/discover.js
var path = require('path');
var fs = require('fs');
var app = require(path.resolve(__dirname, '../server'));
var outputPath = path.resolve(__dirname, '../../common/models');
var dataSource = app.dataSources.mongoDs;
function schemaCB(err, schema) {
if(schema) {
console.log("Auto discovery success: " + schema.name);
var outputName = outputPath + '/' +schema.name + '.json';
fs.writeFile(outputName, JSON.stringify(schema, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputName);
}
});
}
if(err) {
console.error(err);
return;
}
return;
};
dataSource.discoverSchema('CoffeeShop',{schema:'TestLoopBackDB'},schemaCB);
run the above script to discover and build the model [node server/bin/disovery.js] after that in server/model-config.json add this after last object.
"CoffeeShop": {
"dataSource": "mongoDS",
"public": true
}
now run your loopback application.
Upvotes: 1