Sails.js 在SailsJS中,我希望获得特定字段并显示为html
这是我的密码。我在使用find()所有字段时都得到了结果,但我希望使用find和产生错误的where条件。我应该改变什么Sails.js 在SailsJS中,我希望获得特定字段并显示为html,sails.js,sails-mongo,Sails.js,Sails Mongo,这是我的密码。我在使用find()所有字段时都得到了结果,但我希望使用find和产生错误的where条件。我应该改变什么 index : function(req, res, next) { User.find({}).where({userlevel:{'=' : 'admin'}}).exec(function (err, usernames) { if (err) { res.send(400); } else {
index : function(req, res, next) {
User.find({}).where({userlevel:{'=' : 'admin'}}).exec(function (err, usernames) {
if (err) {
res.send(400);
} else {
res.view({
usernames : usernames
});
}
},
我想从mongoDB获取所有管理员用户名
在html中
<% _.each(usernames,function(err,user) { %>
<%= user.usernames %>
<% }) %>
请帮帮我……我是新手试试这个:
index : function(req, res) {
User.find().where({userlevel: 'admin'}).exec(function (err, users) {
if (err) {
res.send(400);
} else {
res.view({
usernames: users
});
}
}
请注意,users
是一个用户列表,而不仅仅是用户名
在你看来:
<% _.each(usernames, function(user) { %>
<%= user.username %>
<% }) %>
我更新了答案。你能和我们分享模型代码吗?api/models/User.js
.attributes:{username:{type:'string'}。在您看来,它应该是user.username
(就像我的回答中那样),而不是user.usernames
。嗨..对不起..属性:{usernames:{type:'string'},…..userlevel:{type:'string'},}。这是正确的。您可以将控制器res.view({usernames:users})
更改为res view({users:users})
?因为您在控制器和视图中都使用了username
,所以它是不明确的。更改后,应检查错误是否保持不变:未定义用户名
<% _.each(usernames, function(user) { %>
<%= user.username %>
<% }) %>