UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ body”

时间:2020-06-23 10:38:04

标签: node.js express async-await passport.js undefined

// XmlExt.cs

using System.Xml;

namespace nrm
{
    public static class XmlExt
    {
        public static XmlElement AppendElement(this XmlDocument element, string qualifiedName, string namespaceURI)
        {
            var newElement = element.CreateElement(qualifiedName, namespaceURI);
            element.AppendChild(newElement);
            return newElement;
        }

        public static XmlElement AppendElement(this XmlNode element, string qualifiedName, string namespaceURI)
        {
            var newElement = element.OwnerDocument.CreateElement(qualifiedName, namespaceURI);
            element.AppendChild(newElement);
            return newElement;
        }
    }
}

您好,我很难找到为什么说const passport = require('passport') const User = require('../models/user') module.exports = async (router) => { router .route('/auth/register') .post( async (req, res) => { if (req.body){ console.log(req.body) //prints Object {username: "lkhjk", password: "asdas", role: 0} let r = await User.createUser(req.body.username, req.body.password. req.body.role) //throws error on this line console.log(r) return res.send(r) } else return res.status(400).send() }) } 在console.log上没有定义的原因,它可以毫无问题地打印出来。 (配置了bodyparser,并在请求中具有正确的标头)

1 个答案:

答案 0 :(得分:0)

您在此行中有一个错字:

let r = await User.createUser(req.body.username, req.body.password. req.body.role)

您有一个.而不是,。因此req.body.password.req是未定义的。

尝试:

let r = await User.createUser(req.body.username, req.body.password, req.body.role)