简体   繁体   English

试图从列表中获取单个项目

[英]trying to get single item from list

I have a fairly bare bones mern stack and im trying to call getUsers and then retrieve a single user from the returned list of users.我有一个相当简单的 mern 堆栈,我正在尝试调用 getUsers,然后从返回的用户列表中检索单个用户。 however using [] doesnt seem to work.但是使用 [] 似乎不起作用。 It looks like getUsers correctly returns the list of users but idk how to pull a single one out看起来 getUsers 正确返回了用户列表,但不知道如何提取一个用户列表

在此处输入图像描述

user-ctrl.js用户-ctrl.js

const User = require('../models/user-model')

createUser = (req, res) => {
    const body = req.body

    if (!body) {
        return res.status(400).json({
            success: false,
            error: 'You must provide a user',
        })
    }

    const user = new User(body)

    if (!user) {
        return res.status(400).json({ success: false, error: err })
    }

    user
        .save()
        .then(() => {
            return res.status(201).json({
                success: true,
                id: user._id,
                message: 'User created!',
            })
        })
        .catch(error => {
            return res.status(400).json({
                error,
                message: 'User not created!',
            })
        })
}

updateUser = async (req, res) => {
    const body = req.body

    if (!body) {
        return res.status(400).json({
            success: false,
            error: 'You must provide a body to update',
        })
    }

    User.findOne({ _id: req.params.id }, (err, user) => {
        if (err) {
            return res.status(404).json({
                err,
                message: 'User not found!',
            })
        }
        user.name = body.name
        user.email = body.email
        user
            .save()
            .then(() => {
                return res.status(200).json({
                    success: true,
                    id: user._id,
                    message: 'User updated!',
                })
            })
            .catch(error => {
                return res.status(404).json({
                    error,
                    message: 'User not updated!',
                })
            })
    })
}

deleteUser = async (req, res) => {
    await User.findOneAndDelete({ _id: req.params.id }, (err, user) => {
        if (err) {
            return res.status(400).json({ success: false, error: err })
        }

        if (!user) {
            return res
                .status(404)
                .json({ success: false, error: `User not found` })
        }

        return res.status(200).json({ success: true, data: user })
    }).catch(err => console.log(err))
}

getUserById = async (req, res) => {
    await User.findOne({ _id: req.params.id }, (err, user) => {
        if (err) {
            return res.status(400).json({ success: false, error: err })
        }

        if (!user) {
            return res
                .status(404)
                .json({ success: false, error: `User not found` })
        }
        return res.status(200).json({ success: true, data: user })
    }).catch(err => console.log(err))
}

getUsers = async (req, res) => {
    await User.find({}, (err, users) => {
        if (err) {
            return res.status(400).json({ success: false, error: err })
        }
        if (!users.length) {
            return res
                .status(404)
                .json({ success: false, error: `User not found` })
        }
        return res.status(200).json({ success: true, data: users })
    }).catch(err => console.log(err))
}

module.exports = {
    createUser,
    updateUser,
    deleteUser,
    getUsers,
    getUserById,
}

You need to actually call the getUsers function (with parenthesis), and then wait for the promise to resolve, with await您需要实际调用getUsers function(带括号),然后等待 promise 解析, await

var allUsers = await UserCtrl.getUsers();
var defaultUser = allUsers[0];

or要么

UserCtl.getUsers()
  .then(u=>u[0])
  .then(user=>{ 
     // insert code that uses the user here 
   })

It's a promise, so try with async/await这是一个 promise,所以尝试使用 async/await

var allUsers = await UserCtrl.getUsers();
var defaultUser = allUsers[0];

To make await work, put async infront of your method:要使 await 工作,请将 async 放在方法的前面:

async createUser = (req, res) => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM