[英]there was error connecting to http://localhost:3000/api/contact
this is my mean code. 这是我的恶意代码。
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;
the above file is route.js. 上面的文件是route.js。 the below code is from contact.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);
});
});
}
I'm trying to post the data from postman it is shoing the error as 我正在尝试从邮递员发布数据,它将错误提示为
"there was error connecting to http://localhost:3000/api/contact "
“连接到http:// localhost:3000 / api / contact时出错”
and in the command prompt it is showing the error as 并在命令提示符下显示错误为
Server started at port3000 connected to mongos database at 27017 Error: Illegal arguments: function, string at _async (D:\\project-1\\back-end\\node_modules\\bcryptjs\\dist\\bcrypt.js:214:46 ) at Object.bcrypt.hash (D:\\project-1\\back-end\\node_modules\\bcryptjs\\dist\\bcry pt.js:220:13) at bcrypt.genSalt (D:\\project-1\\back-end\\models\\contacts.js:49:16) at Immediate._onImmediate (D:\\project-1\\back-end\\node_modules\\bcryptjs\\dist\\ bcrypt.js:153:21) at runCallback (timers.js:794:20) at tryOnImmediate (timers.js:752:5) at processImmediate [as _immediateCallback] (timers.js:729:5) D:\\project-1\\back-end\\models\\contacts.js:54 newContact.save(callback);
服务器在连接到mongos数据库的port3000处以27017启动.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)在tryOnImmediate(timers)的runCallback(timers.js:794:20)的即时(_:project-1 \\ back-end \\ node_modules \\ bcryptjs \\ dist \\ bcrypt.js:153:21) .js:752:5)在processImmediate [as _immediateCallback](timers.js:729:5)D:\\ project-1 \\ back-end \\ models \\ contacts.js:54 newContact.save(callback); ^
^
TypeError: newContact.save is not a function at bcrypt.hash (D:\\project-1\\back-end\\models\\contacts.js:54:23) at runCallback (timers.js:794:20) at tryOnImmediate (timers.js:752:5) at processImmediate [as _immediateCallback] (timers.js:729:5) [nodemon] app crashed - waiting for file changes before starting...
TypeError:newContact.save在tryOnImmediate(timers)的runCallback(timers.js:794:20)的bcrypt.hash(D:\\ project-1 \\ back-end \\ models \\ contacts.js:54:23)处不是函数.js:752:5)在processImmediate [as _immediateCallback](timers.js:729:5)[nodemon]应用崩溃-等待文件更改,然后再开始...
newContact.save(callback); newContact.save(回调); ^ TypeError: newContact.save is not a function.
^ TypeError:newContact.save不是函数。 i don't know why this error is coming.
我不知道为什么会出现这个错误。
You have an issue here: 您在这里遇到问题:
bcrypt
for generating is throwing an error because of wrong parameters. 由于错误的参数,用于生成的
bcrypt
引发错误。 You can't pass object (newContact) to bcrypt
. 您不能将对象(newContact)传递给
bcrypt
。
Try to generate a hash using the following code: 尝试使用以下代码生成哈希:
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(password, salt);
You can use pre save function of mangoose to generate hashedPassword while storing this. 您可以在存储时使用mangoose的pre save功能生成hashedPassword。 Personally, I don't prefer as this adds new check everytime you save the object
就个人而言,我不希望这样做,因为每次保存对象时都会添加新的检查
find() takes query as an argument to search all pass {}
find()将query作为参数来搜索所有通过{}
Contact.find({},function(err, contacts){
res.json(contacts);
})
Contact.addRegistry waits newContact as first parameter, but you do not pass it on your route.js
Contact.addRegistry等待newContact作为第一个参数,但是您不会在route.js上传递它
I think you want to do something like this 我想你想做这样的事情
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();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.