在Sails.js中从MongoDB获取数据

我是Sails.js的新手。 我试图从我的Mongo Db数据库“TestDB”中获取数据,我有一个名为“Components”的集合,所以我创build了一个名为Components的Model,其中包含了我的集合

Components.js

module.exports = { attributes: { TaskId: { type: 'string', required: true }, CompName: { type: 'string' }, InitialAttr: { type: 'string' }, Value: { type: 'string' } } }; 

ComponentsController.js

 module.exports = { GetComponentList : function(req, res){ Components.find({ CompName: 'ImageComponent'}).exec(function(err, data) { if (err) return next(err); res.json(data); }); } }; 

路线:

 '/comp' : { controller: 'components', action: 'GetComponentList' } 

上面的查询在MongoVUE中返回数据集,但返回

 [] 

在Sails.js

Waterline ORM预计所有数据库表/集合都将被缩小。 我猜如果你在你的Mongo数据库看,你会看到现在有两个集合: Componentscomponents 。 如果您不关心数据库中的现有数据,则可以删除Components集合。 否则,您可以使用tableName属性将模型指向现有集合:

 module.exports = { tableName: 'Components', attributes: { TaskId: { type: 'string', required: true }, ...etc... } }