Passionate Coder
Passionate Coder

Reputation: 7294

Data not getting change in node js

I am working with Angular, Node and MySQL. I have a simple crud app in which I am adding,updating some data. Whenever I am adding/updating any data I want it to reflect changes in list grid as well.

I am using popups for add/update. So after adding or updating I am calling my generic function which gives me list of data but I was getting response 304 and changes not displayed in grid. So to overcome 304 I have use app.disable('etag') in Node.js which gives me status 200. But still changes not displayed, but when I refresh page manually I can see the changes.

Also when I am checking data in network just after performing operation it also holds old values.

Node.js

app.post("/addcity", function(req, res) {
    d = req.body;
    var con = mysql.createConnection(connectionString);
    con.connect(function(err) {
        if (err)
            throw err;
        var sql = "INSERT INTO tbl_city(city_name,state_id)  VALUES('" + d.city_name + "'," + d.state_id + ")";
        console.log(sql);
        con.query(sql, function(err, result) {
            if (err)
                throw err;
            res.send(result);
        })
    })
})

Angular code

PostData(addNewForm: NgForm) {
    var object = {
        "city_name": addNewForm.value.city_name,
        "state_id": addNewForm.value.state_id
    }
    this.service.addCity(object).subscribe();
    this.modalRef.hide();
    this.getAllCities(); // this still gives old values even new City is added in database
}

Update

getCities() : Observable<any[]>{
        var details = this.http.get(`${this.apiUrl}/cities`);
        return forkJoin([details]);
    }

Upvotes: 0

Views: 979

Answers (1)

Tuhin Das
Tuhin Das

Reputation: 499

You are calling the two requests in async, that's why by the time the city is added in database. this.getAllCities() gets the list of old cities try it like this.

 PostData(addNewForm: NgForm) {
        var object = {
            "city_name": addNewForm.value.city_name,
            "state_id": addNewForm.value.state_id
        }
        this.service.addCity(object).subscribe(
          (data)=> {
                this.modalRef.hide();
                this.getAllCities();
            }
        ); 
    }

Upvotes: 1

Related Questions