Javascript:为什么对象没有在新的 api 调用中初始化,而字符串变量是?
Javascript: Why object is not getting initialise on new api call however the string variable is?
我可能遗漏了一些基本的东西,比如为什么会这样。
GET: example.com/users
//给出所有数据
GET: example.com/users?status=1
//给出状态为1
的数据
GET: example.com/users
// 这行不通
给出与 status=1
的 pervious API 条件相同的数据
第三次点击时,self.whereObj
没有初始化为默认的空对象,而是采用先前的值 {'status' = '1'}
,但是 self.page
和 self.limit
正在采用默认值,如果查询字符串中没有提供查询参数。
example.com/users?limit=3
, // 覆盖 3
形式的默认值 5
example.com/users
// self.limit
采用默认值 5
,这很好
所以我的问题是为什么 self.limit
(简单字符串变量)正在初始化,而 self.whereObj
不是?
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;
已更新
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
};
So my question is why the self.limit
(simple string variable) is initialising however self.whereObj
is not?
因为对象是参考值。当您设置 var whereObj = self.whereObj;
时,两者都引用同一个对象,并且当您将查询参数复制到对象属性时,您实际上是在写入默认实例。这不会发生在诸如字符串之类的原始值上——它们没有可变属性。
我可能遗漏了一些基本的东西,比如为什么会这样。
GET: example.com/users
//给出所有数据
GET: example.com/users?status=1
//给出状态为1
GET: example.com/users
// 这行不通
给出与 status=1
第三次点击时,self.whereObj
没有初始化为默认的空对象,而是采用先前的值 {'status' = '1'}
,但是 self.page
和 self.limit
正在采用默认值,如果查询字符串中没有提供查询参数。
example.com/users?limit=3
, // 覆盖 3
形式的默认值 5
example.com/users
// self.limit
采用默认值 5
,这很好
所以我的问题是为什么 self.limit
(简单字符串变量)正在初始化,而 self.whereObj
不是?
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;
已更新
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
};
So my question is why the
self.limit
(simple string variable) is initialising howeverself.whereObj
is not?
因为对象是参考值。当您设置 var whereObj = self.whereObj;
时,两者都引用同一个对象,并且当您将查询参数复制到对象属性时,您实际上是在写入默认实例。这不会发生在诸如字符串之类的原始值上——它们没有可变属性。