我是 Sails.js 新手。我正在尝试从我的 Mongo Db 数据库“TestDB”获取数据,我有一个集合名称“Components”,因此我创建了一个名为 Components 的模型,其中包含我的集合的属性
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 数据库,您会发现现在有两个集合:Components
和 components
。如果您不关心数据库中的现有数据,您可以删除 Components
集合。否则,您可以使用 tableName
property 将模型指向现有集合。 :
module.exports = {
tableName: 'Components',
attributes: {
TaskId: {
type: 'string',
required: true
},
...etc...
}
}
关于javascript - 在 Sails.js 中从 MongoDB 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888419/