次のようにポストリクエストを使用してmongodbデータベースにユーザーを保存しようとしていますが、エラーが発生しました。TypeError:Userはコンストラクタではありません。それは非常にシンプルなコードの設定ですが、私はそれに間違って何かを把握することはできません。node.jsのサーバーにオブジェクトを送信中に "TypeError:ユーザーがコンストラクターではありません"
//models/users.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const confic = require('../models/users');
// User schema
const UserSchema = mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
required: true
},
username:{
type: String,
required: true
},
password: {
type: String,
required: true
}
});
const User = module.exports = mongoose.model('User', UserSchema);
module.exports.getUserById = function(id,callback){
User.findById(id,callback);
}
module.exports.getUserByUsername = function(username,callback){
const query = {username:username}
User.findOne(query,callback);
}
module.exports.addUser= function (newUser, callback) {
bcrypt.gensalt(10,(err,salt) => {
bcrypt.hash(newUser.password, salt , (err, hash) =>{
if(err) throw err;
newUser.password=hash;
newUser.save(callback);
});
});
}
ルート/ users.js
//routes/users.js
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
User = require('../models/users');
// // Register
router.post('/register', (req, res, next) => {
var newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, User) => {
if(err){
res.json({success: false, msg:'Failed to register user'});
} else {
res.json({success: true, msg:'User registered'});
}
});
});
// Authenticate
router.post('/authenticate', (req, res, next) => {
res.send('AUTHENTICATE');
});
// Profile
router.get('/profile', (req, res, next) => {
res.send('PROFILE');
});
module.exports = router;
私は、データを送信するためにポストマンクロムを使用していますが、それはプログラムに従って登録したユーザーが表示されていません。
'ユーザーは=(」../設定/データベース ')を要求すべきではない''(ユーザー=必要 'こと。..この行は
変更/models./users ') 'または実際には、あなたが含まれていない別のファイルから再エクスポートすることを意味していますか?いずれにしても、あなたが思うモデルとして「ユーザー」は定義されていません。また、あなたのブラウザの画像よりもポストするのに役立つサーバーエラーログが表示されるはずです。正確になぜ画像を掲載する上で評判キャップがあるのですか?私たちは本当にそれらを必要としないので。 –
"TypeError:bcrypt.gensaltは関数ではありません"しかし、今度はこのエラーを表示します –
アプリケーション全体をデバッグすることはできません。特に実際にエラーが発生している箇所私は他の場所に記録されていると指摘しました。そのログを見てください。実際にエラーが発生した行番号が表示されます。 –