Tag: javascript objects

在MongoDB中使用Mongoose + Node.js存储非结构化的JavaScript对象

在我的node.js应用程序中,我需要在MongoDB中存储非结构化的JavaScript对象。 我在Mongoose中指定了以下模型: module.exports = mongoose.model('DBAllocation', { from: Date, expires: Date, userSession: Object, allocationTimestamp: Date, allocationPriority: Number, vmGroupID: String, allocationRequestContent: Object }); 通过指定userSession和allocationRequestContent的数据types为Objecttypes,我想将JavaScript对象(不指定其结构)保存到MongoDB中并按原样检索它。 但是,当我将模型保存到数据库中时,出现内部错误。 我试图存储以下项目: var allocation = new Allocation({ _id: allocationID, from: Date.now(), expires: null, userSession: authorizedRequest.session, allocationTimestamp: Date.now(), allocationPriority: <some number>, vmGroupID: <some number>, allocationRequestContent: authorizedRequest.requestContent }); authorizedRequest.session和authorizedRequest.requestContent是两个JavaScript对象。 但是当我用{}replace它们时,模型会成功保存。 我听说过我们可以用来存储非结构化数据的strict参数,但是我怀疑是否可以用它来实现我所需要的。 有没有办法来实现这个呢? 任何帮助将非常感激。 更新: 我发现authorizedRequest.session是一个MongoDB模型,我用authorizedRequest.session.toObject()replace了它,并用一个简单的对象{'cat':'123','dog':'456'replaceauthorizedRequest.requestContent […]

在一个console.log中输出string和对象

我正在使用node.js rows是一个对象。 我想看看对象的内容。 console.log("Contents of rows: "); console.log(rows); 上面的代码工作。 然后,我采取了一个捷径和使用 console.log("Contents of rows: " + rows); 现在,这是行不通的。 上述工作是否可以在一条线上进行?

有什么方法可以限制ng-repeat中的重复项目的数量?

有没有什么办法可以限制AngularJS中重复的项目数量? 就像在联系人列表中,我需要input相同的姓名和联系电话号码,但只有2次,当我试图进入第三次时,它显示我一些警告,你已经input联系方式。 我怎样才能通过使用AngularJS或JavaScript来实现这个任务? 我的代码是 <md-list-item ng-show="showContactList" ng-repeat="numbers in contactList track by $index" > <i ng-show="numbers.type == 'test'">textsms</i> <i ng-show="numbers.type == 'CELL' || numbers.type == 'EXT'">phone</i> <img ng-show="numbers.type == 'PAGER'" src="pager.png" width="26"> <div class="md-list-item-text" ng-class="{'md-offset': phone.options.offset }"> <h3>{{ numbers.type }}</h3> <p>{{ numbers.value }}</p> </div> <i ng-click="arrayText.push(numbers);">add</i> </md-list-item> 在上面的代码中,我可以添加N号码来联系姓名和号码。

BitGO – 从对象数组获取帐户和值

我在做什么: 当一个新的用户注册一个个人钱包时,在Mongo内部生成并存储它们。 后来,我们提出请求,并从Mongo获取所有用户钱包地址,并将它们存储在allDBWallets 。 每天生成一个新的钱包,它的地址存储在walletId 。 人们从他们唯一的生成地址向walletId ,我们开始logging和分析所有的walletId交易logging。 // Check if a wallet has new transactions var allTransactions = [ { "id": "09829738672b3aa5be23775ba7ee81fb2f8c99f386c8731bc9710d376698073b", "normalizedHash": "453e4cb85bf5ad12afa13b017e82f3963a420e6b57c59e50098d0b0a8a850493", "date": "2016-06-13T22:51:55.603Z", "fee": 30000, "inputs": [ { "previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f", "previousOutputIndex": 4 }, { "previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f", "previousOutputIndex": 6 }, { "previousHash": "43e5068fddd8c5a2b340524b471aad74755f625ae9d9d6610d132f56c5acbedb", "previousOutputIndex": 0 }, { "previousHash": "c00bd520180c32687ecb9dc00e9d40f918d5b49c752acbe48a39b4b729fc0a03", "previousOutputIndex": 0 } […]

mongoose不保存嵌套的对象

我感到困惑的是为什么mongoose不能拯救我的目标: var objectToSave = new ModelToSave({ _id : req.params.id, Item : customObject.Item //doesn't save with customObject.getItem() neither }); 但是保存这个; 如下所示或使用硬编码值: var objectToSave = new ModelToSave({ _id : req.params.id, Item : { SubItem : { property1 : customObject.Item.SubItem.property1, //also saves with customObject.getItem().SubItem.getProperty1() property2 : customObject.Item.SubItem.property2 } } }); 获得者/设定者是 MyClass.prototype.getItem = function(){ … }; 我的项目对象是相当大的,我宁愿不必指定每一个子属性… 当我查看我的Item对象与console.log(customObject.Item)或当我通过我的API作为JSON返回它,它具有我期望的所有嵌套属性(SubItem,…)。 […]

在HapiJS置信度configuration对象中设置过滤对象的默认值

我在我的项目中使用Confidence库(在HapiJS套件中)进行configurationpipe理(这个configuration文件专门用于WinstonJS传输),而且我想知道是否可以在一个filter中设置单个项目的默认值目的。 我不确定我是否解释了这个问题,我不确定正确的术语是什么,所以inheritance了一个例子: 我目前有一部分configuration设置: module.exports = { $filter: 'env', development: { level: { console: 'debug', file: 'debug' }, path: 'app-core.log' }, staging: { level: { console: 'warn', file: 'warn' }, path: 'app-core.log' }, production: { level: { console: 'error', file: 'error' }, path: 'prod-app-core.log' } } 但是因为所有环境项目的path都应该是相同的,除了production ,我希望能够为path值设置默认值。 像这样的东西: module.exports = { $filter: 'env', development: { […]

为什么'this'在箭头函数中被引用时返回'undefined',但是在匿名函数中被调用时不会被返回?

我正在使用node.js v6.7.0,同时声明一个引用'this'的对象,如果它在一个箭头函数内部,则返回undefined,但当它在一个普通的匿名函数内部时,它会返回对象本身(这就是我想) 例如 let obj = { key: 'val', getScopeWithArrow: () => {return this;}, //returns undefined getScopeWithAnonymous: function() {return this;} //returns the object properly }

将variables内容分配给对象属性名称

我想在node.js中创build一个如下所示的json对象; { WantThisName: { property_length: 8 } } 这是我的代码; var property_name = "WantThisName" var length = 8; var Obj_bin; Obj_bin= {property_name: {property_length:length} }; console.log (Obj_bin); 控制台输出是; { property_name: { property_length: 8 } } 问题在于property_name没有获取variablesproperty_name的内容。

如何在javascript / nodejs中创build静态函数/对象(ES6)

我想用Javascript / Node JS创build一个静态类。 我用谷歌,但我找不到任何有用的例子。 我想在Javascript ES6中创build这样的东西(C#): public static MyStaticClass { public static void someMethod() { //do stuff here } } 现在,我有这个类,但是我认为这个代码每次从“require”调用时都会创build一个新的实例。 function MyStaticClass() { let someMethod = () => { //do some stuff } } var myInstance = new MyStaticClass(); module.exports = factory;