第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

只從 mongoose.find 返回 _id

只從 mongoose.find 返回 _id

冉冉說(shuō) 2024-01-18 16:25:31
想要獲取所有用戶(hù),但只返回 _ids 列表,檢查數(shù)據(jù)庫(kù)中保存的數(shù)據(jù),一切似乎都很好。這是用戶(hù)模型let UserSchema = new mongoose.Schema({  firstName: {    type: String,    minlength: 3,    trim: true,  },  lastName: {    type: String,    minlength: 3,    trim: true,  },  biography: {    type: String,    minlength: 5,    trim: true,  },    }); UserSchema.methods.toJSON = function () {   let user = this;   let userObject = user.toObject();   return _.pick(userObject, ["_id", "firstName", "email"]); };這是我的控制器功能const controller = {    fetchUsers :async (_req, res) => {        try {            await User.find({})            .then((users) => {              res.status(200).send(users);            })            .catch((err) => {              res.status(400).send(err);            });        } catch (error) {          res.status(400).json({            Error: `something is wrong. ${error}`,          });        }      }}我在郵遞員中測(cè)試的結(jié)果是:[    {        "_id": "5fe26ba0d290a216c0fe6d5d"    },    {        "_id": "5fe26c8e40ca9a06b8c96259"    },   ]
查看完整描述

2 回答

?
炎炎設(shè)計(jì)

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊

問(wèn)題是UserSchema.methods.toJSON方法沒(méi)有任何電子郵件字段,如果我們想過(guò)濾輸出數(shù)據(jù),最好通過(guò)mongoose.find({"condition"},{"fields"})



查看完整回答
反對(duì) 回復(fù) 2024-01-18
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

不要同時(shí)使用 .then 和 wait 。嘗試一次。假設(shè)模型是正確的。


const controller = {

    fetchUsers :async (_req, res) => {

        try {

            const users=await User.find({}).exec()

            if(users){

              res.status(200).send(users);

            }

            else{

              res.status(404).send("no user found");

            };

        } catch (error) {

          res.status(500).json({

            Error: `something is wrong. ${error}`,

          });

        }

      }

}


查看完整回答
反對(duì) 回復(fù) 2024-01-18
  • 2 回答
  • 0 關(guān)注
  • 226 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)