【问题标题】:there was error connecting to http://localhost:3000/api/contact连接到 http://localhost:3000/api/contact 时出错
【发布时间】:2018-10-28 19:49:53
【问题描述】:

这是我的平均代码。

const express = require('express');
const router = express.Router();
const passport = require('passport');
const jet = require('jsonwebtoken');

const Contact = require('../models/contacts');


// retrieving Data
router.get('/contacts',(req,res,next)=>{
 // res.send('Retriving the contact list');
 console.log('contacts page');
Contact.find(function(err, contacts){
 res.json(contacts);
})
});

// to add the content 
router.post('/contact',(req, res, next)=>{
// logic to add contact
    let newContact = new Contact({
        first_name: req.body.first_name,
        last_name: req.body.last_name,
        email_id: req.body.email_id,
        password: req.body.password
    });
    Contact.addRegistry((err, contacts)=> {
        if(err) {
            res.json({msg:'faild to add register'});
        }
        else{
            res.json({msg:'registry added sucessfully'});
        }

    });

});    
// to delete the content 
router.delete('/contact/:id',(req, res, next) =>{
    // logic to delete contact
    Contact.remove({_id:req.params.id}, function(err, result){
        if(err){
            res.json(err);
        }
        else {
            res.json(result);
        }
    });
    })    

module.exports = router;

上面的文件是route.js。 以下代码来自contact.js

// Database code.
var express = require('express');
var app = express();     
var mongoose = require('mongoose');     
var bcrypt = require('bcryptjs');             



// database schaema  
var ContactSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    id: String,
    location: String,
    profile_picture_url: String,
    email_id: String,
    phone: String,
    job_title: String,
    company: String,
    education: String,
    password: String,
     savedjobslist: {
        title: [],
        subtitle: []

     },
    appliedjobslist: {
        title: [],
        subtitle: []
    },
    failedjobslist: {
        title: [],
        subtitle: []
    }
});


const Contact = module.exports = mongoose.model('Contact', ContactSchema); 

module.exports.getUserById = function(id,callback) {
    Contact.findById(id,callback);
}
module.exports.getUserById = function(username,callback) {
    const query = {username: username}
    Contact.findOne(query,callback);
}
module.exports.addRegistry = function(newContact,callback) {
    bcrypt.genSalt(10, (err,salt) => {
        bcrypt.hash(newContact,salt, (err,hash) => {
            if (err) {
                console.log(err);
            }
           newContact.password = hash;
           newContact.save(callback);
        });
    });
}

我正在尝试从邮递员那里发布数据,它正在显示错误

“连接到http://localhost:3000/api/contact时出错”

并在命令提示符中显示错误为

服务器在端口 3000 开始,连接到 27017 的 mongos 数据库 错误:非法参数:函数、字符串 在 _async (D:\project-1\back-end\node_modules\bcryptjs\dist\bcrypt.js:214:46 ) 在 Object.bcrypt.hash (D:\project-1\back-end\node_modules\bcryptjs\dist\bcry pt.js:220:13) 在 bcrypt.genSalt (D:\project-1\back-end\models\contacts.js:49:16) 在 Immediate._onImmediate (D:\project-1\back-end\node_modules\bcryptjs\dist\ bcrypt.js:153:21) 在 runCallback (timers.js:794:20) 在 tryOnImmediate (timers.js:752:5) 在 processImmediate [as _immediateCallback] (timers.js:729:5) D:\project-1\back-end\models\contacts.js:54 newContact.save(回调); ^

TypeError: newContact.save 不是函数 在 bcrypt.hash (D:\project-1\back-end\models\contacts.js:54:23) 在 runCallback (timers.js:794:20) 在 tryOnImmediate (timers.js:752:5) 在 processImmediate [as _immediateCallback] (timers.js:729:5) [nodemon] 应用程序崩溃 - 在启动前等待文件更改...

newContact.save(回调); ^ TypeError:newContact.save 不是函数。 我不知道为什么会出现这个错误。

【问题讨论】:

    标签: javascript node.js mean-stack postman


    【解决方案1】:

    find() 以 query 作为参数搜索所有 pass {}

    Contact.find({},function(err, contacts){
    res.json(contacts);
    })
    

    【讨论】:

      【解决方案2】:

      Contact.addRegistry 等待 newContact 作为第一个参数,但您没有在 route.js 上传递它

      我想你想做这样的事情

      ContactSchema.pre('save', function(next) {
        const user = this;
      
        // generate a salt
        bcrypt.genSalt(10, function(err, salt) {
          if (err) return next(err);
      
          // hash your password
          bcrypt.hash(user.password, salt, function(err, hash) {
            if (err) return next(err);
      
            // store hash on the password field
            user.password = hash;
            next();
          });
        });
      });
      

      【讨论】:

        【解决方案3】:

        这里有问题:

        bcrypt 用于生成由于参数错误而引发错误。您不能将对象 (newContact) 传递给 bcrypt

        尝试使用以下代码生成哈希:

        const salt = bcrypt.genSaltSync(10);
        const hashedPassword = bcrypt.hashSync(password, salt);
        

        您可以在存储时使用pre save function of mangoose 生成 hashedPassword。就个人而言,我不喜欢这样,因为每次保存对象时都会添加新的检查

        【讨论】:

          猜你喜欢
          • 2021-09-28
          • 1970-01-01
          • 2014-04-23
          • 2019-07-07
          • 2016-07-21
          • 2021-03-11
          • 2018-11-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多