I want to save req(Request) data in app.get('/') to something. It occurred "TypeError: Converting circular structure to JSON"
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.get('/', (req, res) => {
var string = JSON.stringify(req);
saveRequest(string)
res.send("OK")
})
function saveRequest(){
//...
}
Did you know req data to string? I've already try this code
app.get('/', (req, res) => {
var string = objToString(req);
saveRequest(string)
res.send("OK")
})
function objToString (obj) {
var str = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + '\n';
}
}
return str;
}
it occurred "TypeError: Cannot convert object to primitive value"
I want to save req(Request) data in app.get('/') to something. It occurred "TypeError: Converting circular structure to JSON"
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.get('/', (req, res) => {
var string = JSON.stringify(req);
saveRequest(string)
res.send("OK")
})
function saveRequest(){
//...
}
Did you know req data to string? I've already try this code
app.get('/', (req, res) => {
var string = objToString(req);
saveRequest(string)
res.send("OK")
})
function objToString (obj) {
var str = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + '\n';
}
}
return str;
}
it occurred "TypeError: Cannot convert object to primitive value"
Share Improve this question asked Sep 8, 2018 at 2:56 You.BrightonYou.Brighton 2,0982 gold badges18 silver badges27 bronze badges1 Answer
Reset to default 7I'm not sure why you'd want to save the whole request object but there are node modules available to safely stringify objects that contain circular references such as json-stringify-safe.
Heres an example
let app = require('express')();
let port = process.env.PORT || 3000;
let stringify = require('json-stringify-safe');
app.get('/', (req, res) => {
console.log(stringify(req));
res.send("Ok");
})
app.listen(port, () => {
console.log("Listening on " + port);
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190792a4562423.html
评论列表(0条)