我在api nodejs上有一个方法发布,而在angular上有一个客户端应用程序,当我在邮递员上测试方法发布时,这项工作有效,但是当我从客户端应用程序发送数据时,它不起作用:
这是我的代码nodejs api:
'use strict'
var historial = require('../models/historial');
var controller={
saveHistorial: function(req, res){
console.log("entro")
var Historial = new historial();
var params = req.body;
Historial.nombre= params.nombre;
Historial.cantidad= params.cantidad;
Historial.unidadMedida= params.unidadMedida;
Historial.fecha= params.fecha;
Historial.costo= params.costo
Historial.save((err, HistorialStored)=>{
if(err)return res.status(500).send({message: 'error en la peticion'});
if(!HistorialStored) return res.status(404).send({message: 'no se ha podido guardar'});
return res.status(200).send({Historial: HistorialStored});
});
},
getHistorial: function(req, res){
historial.find({}).exec((err, Historial)=> {
if(err) return res.status(500).send({message: 'error al devolver los datos'});
if(!Historial) return res.status(404).send({message:'No hay Historials'});
return res.status(200).send({Historial});
});
},
};
module.exports = controller;
这是我对angular的服务:
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {historial} from '../models/historial';
import {Global} from './global';
@Injectable({
providedIn: 'root'
})
export class HistorialService {
public url: string;
constructor(private _httpclient:HttpClient ) {
this.url= Global.url;
}
saveRegistro(_historial:historial): Observable<any>{
let params = JSON.stringify(_historial);
let headers = new HttpHeaders().set('Content-Type', 'application/json');
debugger
return this._httpclient.post(this.url + 'save-registro', params, {headers: headers});
}
getHistorial():Observable<any>{
let headers = new HttpHeaders().set('Content-Type', 'application/json');
return this._httpclient.get(this.url+'historial', {headers:headers});
}
}
当我执行此服务时,服务器上的控制台上会出现此错误:
(node:11340) DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.