atjoshi atjoshi - 16 days ago 6
Node.js Question

Javascript: Why object is not getting initialise on new api call however the string variable is?

I may be missing something basic as why is it happening.

GET: example.com/users

//gives all data

GET: example.com/users?status=1

//gives data with status = 1

GET: example.com/users
// this does not work
gives same data as pervious API condition with status=1

On third hit,
self.whereObj
is not initialising to default empty object instead it takes previous value of
{'status' = '1'}
, however
self.page
and
self.limit
is taking default value if no query parameter is provided in query string.

example.com/users?limit=3
, // takes override to
3
form default value of
5


example.com/users
//
self.limit
takes default
5
and this works fine

So my question is why the
self.limit
(simple string variable) is initialising however
self.whereObj
is not ?


var Bookshelf = require('../../dbconfig').bookshelf;
Bookshelf.Collection = Bookshelf.Collection.extend({

limit: 5,
page: 1,
whereObj: {}

myFetch: function (query_params,expectedWhereFields) {
var self = this;
var whereObj = self.whereObj ; // this is not initializing
// var whereObj = {}; this is initialising
var page = self.page;
var limit = self.limit; //this is not showing nay initialisation error

for (var x in query_params) {
if (expectedWhereFields.includes(x)) {
whereObj[x] = query_params[x];
}
if (x === 'page') {
page = query_params[x];
}
if (x === 'limit') {
limit = query_params[x];
}
}
var offset = (page - 1) * limit;

function fetch() {
return self.constructor.forge()
.query({where: whereObj})
.query(function (qb) {
qb.offset(offset).limit(limit);
})
.then(function (collection) {
return collection;
})
.catch(function (err) {
return err
});
}
return new fetch();
}
});
module.exports = Bookshelf;


UPDATED

service.js

var Model = require('./../models/Users');
var express = require('express');

var listUsers = function (query_params, callback) {
var expectedWhereFields = ["type", "status", "name"];
Model.Users
.forge()
.myFetch(query_params, expectedWhereFields)
.then(function (collection) {
return callback(null, collection);
})
.catch(function (err) {
return callback(err, null);
});
};

module.exports = {
listUsers: listUsers
};


model/Users.js

var Bookshelf = require('../../dbconfig').bookshelf;
var Base = require('./base');

// Users model
var User = Bookshelf.Model.extend({
tableName: 'user_table'
});
var Users = Bookshelf.Collection.extend({
model: User
});

module.exports = {
User: User,
Users: Users
};

Answer

So my question is why the self.limit (simple string variable) is initialising however self.whereObj is not?

Because objects are reference values. When you set var whereObj = self.whereObj;, both refer to the same object, and when you copy the query parameters into the object properties you are effectively writing into your defaults instance. This does not happen with primitive values such as strings - they don't have mutable properties.

Comments