OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

node axios post undefiend error is problem [closed]

  • Thread starter Thread starter 정채림
  • Start date Start date

정채림

Guest
/models/user.js

Code:
exports.postUser = (data, callback) => {
  // 매개변수
  // data : 프론트엔드에서 유저가 입력한 값 ( req.body )
  // callback :  query 실행 후 호출할 함수

  conn.query(
    `insert into user values(null, "${data.nickname}", "${data.user_id}")`,
    (err, rows) => {
      if (err) {
        throw err;
      }
      console.log("model >> ", rows);
      callback(rows[0]);
    }
  );
};


/controllers/userController.js

exports.postUsers = (req, res) => {
    User.postUser((result)=>{
        console.log("post Users", result);
        res.json(result);
    })
}

register page

Code:
const handleRegister = async () => {
    console.log("전송할 데이터:", {user_id: user_id, nickname: nickname});
    if (isIdValid && isNameValid && user_id !== "" && nickname !== "") {
      try {
        const response = await axios.post(`${apiUrl}/users`, {
          user_id,
          nickname,
        });

        setIsRegistered(true);
        alert("회원가입 성공!");

        console.log("회원가입 성공", response.data);
        setTimeout(() => {
          router.push("/login");
        }, 2000); // 2초 후 로그인 페이지로 이동
      } catch (error) {
        console.log("전송할 데이터:", {user_id, nickname});
        console.error("회원가입 중 오류가 발생했습니다:", error);
        alert("회원가입에 실패했습니다. 다시 시도해 주세요.");
      }
    }
  }

Code:
model >>  OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 34,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

Error Message

Code:
 throw err; // Rethrow non-MySQL errors
      ^

TypeError: callback is not a function

I don't know why undefined is insert data..

{"id":33,"nickname":"undefined","user_id":"undefined"}]

I tried to callback(rows[0]) and there's no more...

<p>/models/user.js</p>
<pre><code>exports.postUser = (data, callback) => {
// 매개변수
// data : 프론트엔드에서 유저가 입력한 값 ( req.body )
// callback : query 실행 후 호출할 함수

conn.query(
`insert into user values(null, "${data.nickname}", "${data.user_id}")`,
(err, rows) => {
if (err) {
throw err;
}
console.log("model >> ", rows);
callback(rows[0]);
}
);
};


/controllers/userController.js

exports.postUsers = (req, res) => {
User.postUser((result)=>{
console.log("post Users", result);
res.json(result);
})
}
</code></pre>
<p>register page</p>
<pre><code>const handleRegister = async () => {
console.log("전송할 데이터:", {user_id: user_id, nickname: nickname});
if (isIdValid && isNameValid && user_id !== "" && nickname !== "") {
try {
const response = await axios.post(`${apiUrl}/users`, {
user_id,
nickname,
});

setIsRegistered(true);
alert("회원가입 성공!");

console.log("회원가입 성공", response.data);
setTimeout(() => {
router.push("/login");
}, 2000); // 2초 후 로그인 페이지로 이동
} catch (error) {
console.log("전송할 데이터:", {user_id, nickname});
console.error("회원가입 중 오류가 발생했습니다:", error);
alert("회원가입에 실패했습니다. 다시 시도해 주세요.");
}
}
}
</code></pre>
<pre><code>model >> OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 34,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
</code></pre>
<p>Error Message</p>
<pre><code> throw err; // Rethrow non-MySQL errors
^

TypeError: callback is not a function
</code></pre>
<p>I don't know why undefined is insert data..</p>
<p>{"id":33,"nickname":"undefined","user_id":"undefined"}]</p>
<p>I tried to callback(rows[0]) and there's no more...</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top