现在我在 Ionic 3 中开展示例项目,这个项目是 CRUD (创建,读取,更新,删除)。
我有一个带有流明的API,并且所有端点都运行良好,我使用 Postman 进行了测试。这是路由和控制器代码。
路线
$router->group(['prefix' => 'visitas'], function () use ($router) {
$router->get('/', [
'as' => 'visitas.read', 'uses' => 'VisitaController@read'
]);
$router->get('/{id}/edit', [
'as' => 'visita.edit', 'uses' => 'VisitaController@edit'
]);
$router->delete('/{id}', [
'as' => 'visita.destroy', 'uses' => 'VisitaController@destroy'
]);
$router->put('/{id}', [
'as' => 'visita.update', 'uses' => 'VisitaController@update'
]);
$router->post('/create', [
'as' => 'visita.create', 'uses' => 'VisitaController@create'
]);
});
控制器
public function create(Request $request)
{
$this->validate($request, [
'nombre' => 'required',
'dni' => 'required',
'visitado_id' => 'required'
]);
Visitas::create($request->all());
}
在Ionic中我有一个提供者
createVisita(data) {
console.log(data);
return this.http.post('http://visitas.api.app:8000/visitas/create/',data)
.map(res => res.json())
.toPromise();
}
控制器看起来像这样
newVisita() {
this.visitasCtrl.createVisita(this.create)
.then(createData => {
this.createData = createData;
})
this.navCtrl.pop();
}
This.create
检索所有表单数据,但不保存并返回url错误。我认为这是一个承诺(或类似的东西)错误但不确定。我需要在数据库中执行此插入操作,然后使用pop().
¿任何想法都有什么问题?
答案 0 :(得分:0)
****更新了正确的代码**
导入ReactiveJS库
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/finally';
createVisita(data) {
return Observable.create((observer: any) => {
console.log(data);
return this.http.post('http://visitas.api.app:8000/visitas/create/', data)
.map((res) => {
callback = res.json();
observer.next(callback);
})
});
};
newVisita() {
this.visitasCtrl.createVisita(this.create)
.subscribe(createData => {
this.createData = createData;
};
this.navCtrl.pop();
}