簡體   English   中英

next.js/react 中 JSON 輸入意外結束

[英]Unexpected end of JSON input in next.js/react

我有以下代碼:

import connectDB from '../../../lib/mongodb'
import crypto from 'crypto'
import User from '../../../models/user'

const handler = async (req, res) => {
  const body = JSON.parse(req.body);

  try {
    // Hash password to store it in DB
    var hash = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex')

    var user = new User({
      firstname: body.firstname,
      lastname: body.lastname,
      username: body.username,
      email: body.email,
      password: hash
    })

    // Create new user
    var usercreated = await user.save();
    return res.status(200).send(usercreated);
  } catch (error) {
    return res.status(500).send(error.message);
  }
  
  return res.status(400).send({message: 'missing required data'})
}

export default connectDB(handler);

但是,我似乎收到了一個錯誤error - SyntaxError: Unexpected end of JSON input - 沒有什么會跳出錯誤或語法明智的,所以任何幫助都會很棒哈哈。

編輯

這里要求的是用戶模型和注冊頁面

用戶模型

import mongoose from 'mongoose'
const Schema = mongoose.Schema

const user = new Schema({
  email: { type: String, required: true, unique: true },
  password: String,
  username: { type: String, required: true, unique: true },
  admin: { type: Boolean, default: false },
  firstname: { type: String, required: true },
  lastname: { type: String, required: true },

  // reset password
  resetPasswordToken: String,
  resetPasswordExpires: Date
});

mongoose.models = {}

const User = mongoose.model('user', user)

export default User;

這是注冊頁面

import { useState } from 'react'
import Head from 'next/head'
import Link from 'next/link'
import Image from 'next/image'

export default function Register() {
  const [firstname, setFirstName] = useState('')
  const [lastname, setLastName] = useState('')
  const [email, setEmail] = useState('')
  const [username, setUsername] =  useState('')
  const [password, setPassword] = useState('')

  const handleSubmit = e => {
    e.preventDefault();

    const data = {
      firstname,
      lastname,
      email,
      username,
      password
    };

    fetch('/api/auth/register'), {
      method: 'POST',
      body: JSON.stringify(data)
    }

    console.log(data);
  }

  return (
    <>
      <div className="auth-pages">
        <aside></aside>

        <div className="container">
          <form onSubmit={handleSubmit}>
            <h1>Register</h1>

            <label htmlFor="firstname">First Name</label>

            <input 
              id="firstname"
              name="firstname"
              type="text"
              onChange={e => setFirstName(e.target.value)}
              required
            />

            <label htmlFor="lastname">Last Name</label>

            <input
              id="lastname" 
              type="text" 
              name="lastname"
              onChange={e => setLastName(e.target.value)}
              required
            />

            <label htmlFor="email">Email Address</label>

            <input
              id="email" 
              type="email" 
              name="email"
              onChange={e => setEmail(e.target.value)}
              required
            />

            <label htmlFor="username">Username</label>

            <input 
              id="username"
              type="text" 
              name="username"
              onChange={e => setUsername(e.target.value)}
              required 
            />

            <label htmlFor="password">Password</label>

            <input
              id="password" 
              type="password" 
              name="password"
              onChange={e => setPassword(e.target.value)}
              required
            />
            
            <div className="btn-group">
              <button type="submit" value="submit">Register</button>
              
              <div>
                <Link href="/dashboard/auth/login">
                  <a>Login</a>
                </Link>
              </div>
            </div>
          </form>
        </div>
      </div>
    </>
  )
}

當我提交表單時,我可以在反應開發工具中看到狀態隨着數據更新,並且數據通過獲取請求中的console.log(data)顯示。

從用戶架構內部刪除// reset password注釋。

模式輸入是 json 並且 json 不支持其中的注釋。


const user = new Schema({
  email: { type: String, required: true, unique: true },
  password: String,
  username: { type: String, required: true, unique: true },
  admin: { type: Boolean, default: false },
  firstname: { type: String, required: true },
  lastname: { type: String, required: true },
  resetPasswordToken: String,
  resetPasswordExpires: Date
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM