2017-07-21 8 views
1

APIを作成し、それをAngular 2サービスで呼び出すことにより、nodejs内のidに基づいて製品を更新しています。ノードJs findByIdAndUpdate関数がdb内で更新されていない

ルート/ products.js

router.post('/:id', function(req, res, next) { 

Products.findByIdAndUpdate(req.params.id, req.body, function (err, post) { 
if(err){ 
    res.json({success: false, msg:'Failed updating products'}); 
    } else { 
    res.json({success: true, msg:'success updating products'}); 

    } 
}); 
}); 

これはどのようにイム郵便配達で通過している:POSTリクエスト

{ 
"id":"596df0ffcb429a586ef6c0bf", 
"category_id": "596ddb165314d15618795826", 
"product_name":"Laysssss masala", 
"product_price":5, 
"product_image":"image1ddd", 
"product_desc":"Yummyddsd", 
"product_status":"Activedddd" 
} 

製品は、DBに保存されているように見えます。

私が角度の下でサービスを呼び出してブラウザで試してみると、同じことがbroswerでは "Product updated successfully"と表示されますが、DBには反映されません。

component.tsここ

onSaveConfirm(event) { 
if (window.confirm('Are you sure you want to save?')) { 
// var prodId = event.newData['_id'] 
    //console.log(event.newData) 
    this.productService.productUpdate(event.newData).subscribe(data => { 
    if (data.success) { 
     console.log('Edited success') 
     this.flashMessage.show('Product updated successfully', { cssClass: 'alert-success', timeout: 3000 }); 
     this.router.navigate(['/category-items']); 
    } else { 
     console.log('failure edited...') 
     this.flashMessage.show('Something went wrong', { cssClass: 'alert-danger', timeout: 3000 }); 
     this.router.navigate(['/category-items']); 
    } 
    }); 
    event.confirm.resolve(event.newData); 
} else { 
    event.confirm.reject(); 
} 

} 

product.service.ts

productUpdate(products) { 
var prodId = products['_id']; 
console.log(products) 
let prodUrl = "http://localhost:8080/products/" + prodId 
let headers = new Headers(); 
headers.append('Content-Type', 'application/json'); 
let prodIdObj = JSON.stringify({ products: products }) 
return this.http.post(prodUrl, prodIdObj, { headers: headers }) 
    .map((response: Response) => response.json()) 
    .do(data => JSON.stringify(data)) 
    .catch(this.handleError); 
} 
+0

component.tsでdata' 'の値は何ですか? – Bhavana

+0

データがあります... Object {success:true、msg: "製品の更新を成功させました"} – phyme

+0

この@Bhavanaに関する提案 – phyme

答えて

0

多分あなたの問題:product.service.ts

productUpdate(products) { 
//skipped 
let prodUrl = "http://localhost:8080/products/" + prodId 
//skipped 
} 

products.js

router.post('/:id', function(req, res, next) { 

    Products.findByIdAndUpdate(req.params.id, req.body, function (err, post) { 
     if(err){ 
      res.json({success: false, msg:'Failed updating products'}); 
     } else { 
     res.json({success: true, msg:'success updating products'}); 

     } 
    }); 
}); 

に変更し、それを:

router.post('products/:id', function(req, res, next) { 

    //do something 
}); 
+0

URLは正しいです@Burdy、routes/products.js、およびproducts.jsファイルはhttp post '/(後に何かがある)' ...そうだと私は正しいと信じている! – phyme

関連する問題