型号:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
email: {
type: String,
unique: true,
required: true,
trim: true
},
username: {
type: String,
unique: true,
required: true,
trim: true
},
password: {
type: String,
required: true,
},
passwordConf: {
type: String,
required: true,
},
followedUsers: {
type: Array,
},
avatar: {
type: String,
required: false,
},
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
获取请求:
app.get('/users', (request, response) => {
User.find({}, 'handle users', function (error, users) {
if (error) console.log(error);
response.send({
users: users
})
})
});
运行此命令时,只会让_id
回来:
{
users: [{
_id: "5c097129f3c87328ff608c0d"
},
{
_id: "5c097140f3c87328ff608c0e"
},
{
_id: "5c0a1b17c1613117d41d8846"
},
{
_id: "5c0a1b90c9ecbc17f3c7b98f"
}
]
}
当我查看mongodb时,一切正常。我在做什么错了?
答案 0 :(得分:0)
感谢@abskmj
get-request必须看起来像这样:
app.get('/users', (request, response) => {
User.find({}, 'email username password passwordConf followedUsers avatar', function (error, users) {
if (error) console.log(error);
response.send({
users: users
})
})
});
答案 1 :(得分:0)
像这样编写代码
app.get('/users', (request, response) => {
User.find({}, function (error, users) {
if (error) console.log(error);
response.send({
users: users
})
})
});
或特定字段
enter code here
app.get('/users', (request, response) => {
User.find({}, 'email name', function (error, users) {
if (error) console.log(error);
response.send({
users: users
})
})
});