Tag: json

如何在使用节点js mongoose保存后返回json数据

使用post方法使用save函数插入数据,在同一个方法中需要得到和我们插入的id相同的json数据 apiRoutes.post('/doctor', function(req, res){ if(!req.body.Name || !req.body.password){ res.json({success: false, msg: 'please pass the username and password'}); }else{ var newUser = new Doctor({ Name:req.body.Name, password : req.body.password, }); newUser.save(function(err){ if(err){ res.json({success: false, msg :'username alredy existes'}); }else{ res.json({success: true, msg : 'Successfull created user'}); } }); } }); 在res.json中需要返回与documnet的_id相同的文件名和密码

预期数组,但收到…(JSON)… AngularJS错误

我正在从Adam Freeman的“Pro AngularJS”一书中学习Angular1,在第6-8章中介绍了构buildDeployD应用程序的问题 – 好像我的代码不想读取JSON 这是我的HTML: <!DOCTYPE html> <html ng-app="sportsStore" lang="pl"> <head> <title>SportsStore</title> <script src="components/angular.js"></script> <script src="components/angular-resource.js"></script> <link href="components/bootstrap.css" rel="stylesheet" /> <link href="components/bootstrap-theme.css" rel="stylesheet" /> <script> angular.module("sportsStore", ["customFilters"]); </script> <script src="controllers/sportsStore.js"></script> <script src="filters/customFilters.js"></script> <script src="controllers/productListControllers.js"></script> </head> <body ng-controller="sportsStoreCtrl"> <div class="navbar navbar-inverse"> <a class="navbar-brand" href="#">SKLEP SPORTOWY</a> </div> <div class="panel panel-default row" ng-controller="productListCtrl"> <div class="alert alert-danger" ng-show="data.error"> […]

访问callback函数外的node.js中由readline&fsparsing的数据

这个问题是不同的,因为已经有一个答案。 具体来说,这段代码从node.js文档中调用,关于使用fs和readfile,并查找文件标志的结尾,我学到的是readfile.close方法。 感谢你的回答。 我在本地编写了一个小工具,试图用一个空白行将key:value对的文本文件转换成一个JSON文件,以便在React项目中使用。 我直接从node.js文档中获得了readline函数的基础。 我在我的Mac上使用节点6.9.0 这里是完整的脚本: const readline = require('readline'); const fs = require('fs'); const rl = readline.createInterface({ input: fs.createReadStream('usat-ncaa-programs.txt') }); var newPairs=["test"]; rl.on('line', function (line) { if (line===null){ newPairs.push("}], [ {") } else if (line) { var keyValue = line.match(/^(.*):(.*)/) var newKeyValuePair = "'" + keyValue[1].trim() + "': '" + keyValue[2].trim() + "'" […]

如何在mongo db中拼合子文档

我的collections有这样的文件 { "_id" : ObjectId("587c8d0364b6e32706f7edef"), "first_name" : "John", "last_name" : "Doe", "password" : "aasdjsabb12213b21bbcghc1h2", "shift" : "A", "dept" : "Management" "Requests" : [ { "weekId" : 1, "MO" : 1, "TU" : 2, "W" : 3, "TH" : 9, "FR" : 10, "SA" : 6, "SU" : 1 } ] } 我想将我的查询结果导出到csv,并需要像这样平铺的字段 { "_id" : […]

res.json返回无意修改的input

我的res.json函数似乎在修改我的数据。 如果我在函数中logging数据,它将返回正确的数据。 只有在res.json里面我的数据改变了,我不知道为什么。 例如。 它返回{"unix":"1484600306","naturalFormat":"2017-01-16"}它返回{"unix":"1484600306","naturalFormat":"\"2017-01-16\""}. function: function unixToDate(timestamp) { var a = new Date(timestamp * 1000); //console.log(a); var rgx = /T(\d{2}):(\d{2}):(\d{2}).(\d{3})Z/; var newA = JSON.stringify(a); //console.log(newA.replace(rgx, "")); return newA.replace(rgx, ""); } 路线 router.get('/:unix', function(req, res) { var timestamp = req.params.unix; var regex = new RegExp("\\d{10}"); if (regex.test(timestamp)) { var date = unixToDate(timestamp); console.log(date); res.json({ unix […]

如何将json对象的数组保存到mongoose?

我试图保存对象的mongoose模式的数组,但它是无法加载数据到laptop架构,我只看到一个插入包含id 。 我怎样才能将json对象的数组保存到mongoose模式? controller.js var laptopData = [ { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'HP'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: 'Hp probook 15.1',cpu:'2.4GHZ' ,ram: '4GB', harddrive: '160GB',Brand:'Hp'}, { name: […]

'无法从节点js中的JSON对象获取值

我有一个jsp文件,使用下面的代码返回一个json对象: JSONObject object = new JSONObject(); object.put("name","domain"); object.put("email","domain.com"); response.setContentType("application/json"); response.getWriter().write(object.toString()); 输出结果是:{“name”:“domain”,“email”:“domain.com”} 我试图从node.js中使用下面的代码从这个JSON获取值: var endpoint = // contains the address of the above jsp file; var body = "" http.get(endpoint, (response) => { response.on('data', (chunk) => { body += chunk }) response.on('end', () => { console.log("Body: "+body); console.log("Body name: "+body.name); }) }) 在上面的代码片段中,我得到了console.log的输出结果 – 正文:{“name”:“domain”,“email”:“domain.com”} 身体名称:未定义 […]

parsing从Netsuite到Heroku的JSON HTTP请求会返回错误“SyntaxError:Unexpected token p”

我创build了一个接受参数的Portlet表单脚本,将其提交给适当的Suitelet,然后将生成的JSON从Netsuite转发到Node.js Heroku服务器以供进一步处理。 我创build的JSON是好的,并返回正确的值,但是当它发送请求,heroku日志给我的错误SyntaxError: Unexpected token p 。 奇怪的是,当我在本地运行服务器并通过邮递员发送时,所有这些工作都很好。 当我通过Netsuite请求发送所有内容时,会造成问题。 它也可能是body-parser的错误引用body-parser的东西。 我觉得这个答案是正确的,但最终并没有帮助。 任何想法如何纠正? 处理JSON和请求的Suitelet POST请求: var json = { "par": { "id":request.getParameter("custpage_id"), "num":request.getParameter("custpage_num") }, "in": { "headers":temp.toString() } }; for (var i = 0; i < info.length; i++){ var newVal = "val" + (i+1); json.in[newVal] = info[i]; } nlapiRequestURL("https://somelink.herokuapp.com/", json, {"Content-Type":"application/json"}); Heroku代码: app.use(cors()); app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); […]

Node.js从mongodb呈现json地图

我想从json地图显示数据来表示 这是我的代码 var b = det.map(function(h) { var soalid = h.id_soal; var idarray = [ ]; for (var i = 0; i < soalid.length; i++) { Soal.findOne({ _id: soalid[i] }, function (err, sol) { idarray.push(sol); console.log("ping:" + idarray); }); } res.render('ujian/viewDetail',{ujian: iduji, detail: det, soal: idarray, title: 'Lihat form ujian'}); }); 我试着运行控制台我从JSON获取数据,但没有显示在快递(networking)。 我有这个json文件: ping:{ _id: […]

Express Multiple res.json(); 与中间件

我正在尝试使用JWT构build一个无状态的API。 我在我的路由器中有一个中间件function,它检查JWT是否已经过期,如果有的话,一个带有新时间戳的新令牌在飞行中产生。 我想传递新的标记以及JSON对象中的响应。 我目前的做法是这样的,但它当然不起作用: router.use(function (req, res, next) { // JWT Expired // Generate new JWT res.write(JSON.stringify({token: token}); next(); }); router.get('/securedRoute' function(req, res) { // Fetch data res.json({data: data}); }); : // Output should be: {token: 'erg34jlkgjre.3f4fknlj.34f3', data: ['sdfa', 'sdf']} 这将是很好的find一种方式,我不必改变我现有的所有代码,并检查是否有新的令牌。 谢谢你的帮助!