onSubmit(formValue: any) {
console.log("Form Value = " + JSON.stringify(formValue, null, 4));
let newRegisterdUser = {
firstName : formValue.firstName,
lastName : formValue.lastName,
email : formValue.email,
password : formValue.password,
phone : formValue.phone,
location : formValue.location
};
console.log('Registered user in Angular : '+JSON.stringify(newRegisterdUser, null, 4));
this._registrationService.saveRegisteredUser(newRegisterdUser).subscribe(
(test : any)=> this.router.navigate(['registrationsuccess']),
err => console.log(err)
);
}
import { Injectable } from "@angular/core";
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { Router } from '@angular/router';
@Injectable()
export class RegistrationService{
constructor(private _http : HttpClient){}
saveRegisteredUser(newRegisterdUser : any){
console.log('test user : '+newRegisterdUser.firstName);
return this._http.post('http://localhost:3000/register/save',newRegisterdUser);
}
}
var RegisteredUserApi = require('../data/FoodCourtApi');
var express = require('express');
var router = express.Router();
router.get('/', function(req,res){
res.json({Hi : 'karthik'});
});
router.post('/save', function (req, res) {
var registeredUser = {};
registeredUser.firstName = req.body.firstName;
registeredUser.lastName = req.body.lastName;
registeredUser.email = req.body.email;
registeredUser.password = req.body.password;
registeredUser.phone = req.body.phone;
registeredUser.location = req.body.location;
// console.log('Registered user in Node : '+JSON.stringify(registeredUser, null, 4));
RegisteredUserApi.saveRegisteredUser(registeredUser, function (err, registeredUser) {
// res.end();
if(err){
console.log('Hii');
console.log(err);
res.status(500).json({ error: "save failed", err: err});
}else{
res.json([{ statusSave: 'success' }]);
}
});
});
var RegisteredUserApi = {
saveRegisteredUser: function(registeredUser, callback) {
var newUser = new RegisteredUser(registeredUser);
newUser.save({}, function (err, registeredUser) {
if (err) {
return console.log(err);
} else {
return console.log('RegisteredUser inserted successfully.....!!!');
}
});
}
}
触发两个网络呼叫,一个使用OPTIONS,另一个使用POST。选项网络呼叫状态代码为204,没有请求有效负载且没有响应内容。 POST网络呼叫正在接收请求有效负载,并在一段时间后(大多数在3分钟后)失败。但是在后端(Node& Mongo)中,我看到正在插入2倍的记录。
答案 0 :(得分:2)
在
saveRegisteredUser: function(regis...
保存用户后需要调用callback
函数。
而不是return console.log(err);
和return console.log('RegisteredUser inserted successfully.....!!!');
中的console.log使用callback(....)
您的快递应用根本没有回复/save
路线的任何请求。此代码res.status(500).json({ error..
或此res.json([{ statu....
均未被调用。
var RegisteredUserApi = {
saveRegisteredUser: function(registeredUser, callback) {
var newUser = new RegisteredUser(registeredUser);
newUser.save({}, function (err, registeredUser) {
if (err) {
return callback(err); // <---
} else {
return callback('RegisteredUser inserted successfully.....!!!'); // <---
}
});
}
}