I'm getting this error on a POST method, but im pretty sure im doing it right..
Error:
Unexpected token f in JSON at position 1
at JSON.parse
This makes sense sort of...well 'token f' makes sense, thats what my data starts with: field1: "##########"
heres my api's post function:
var putNewNums = (data) => {
fetch('/getnums', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
})
.then(res => res.send())
}
which goes to this route:
app.use('/getnums', getRouter)
which goes to this controller:
router.post('/', mainController.postNums)
which looks like this:
postNums: (req, res, next) => {
numSchem.create({
field1: req.body.field1
})
.then(data => res.status(200).json(data))
.catch(e => {
req.error = e
console.log(e)
next()
})
}
not exactly sure where my error is..im sending it a string just like my schema wants:
const nums = mongoose.Schema ({
field1: String,
})
Any ideas on what I'm doing wrong?
EDIT:
here is the error i am now getting after changing req.boy to req.body.field1 in my postNums function:
{ ValidationError: numSchema validation failed: field1: Cast to String failed for value "{ field1: '8773238968' }" at path "field1"
at ValidationError.inspect (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/error/validation.js:59:24)
at formatValue (util.js:400:38)
at inspect (util.js:294:10)
at format (util.js:161:12)
at Console.log (console.js:130:21)
at numSchem.create.then.catch.e (/Users/reid/Desktop/DupeFinder/server/controllers/main.js:47:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
errors:
{ field1:
{ CastError: Cast to String failed for value "{ field1: '8773238968' }" at path "field1"
at new CastError (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/error/cast.js:29:11)
at model.$set (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/document.js:879:7)
at model._handleIndex (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/document.js:688:14)
at model.$set (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/document.js:645:22)
at model.Document (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/document.js:112:12)
at model.Model (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/model.js:69:12)
at new model (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/model.js:4344:13)
at toExecute.push.callback (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/model.js:2479:22)
at /Users/reid/Desktop/DupeFinder/server/node_modules/async/internal/parallel.js:31:39
at eachOfArrayLike (/Users/reid/Desktop/DupeFinder/server/node_modules/async/eachOf.js:65:9)
at exports.default (/Users/reid/Desktop/DupeFinder/server/node_modules/async/eachOf.js:9:5)
at _parallel (/Users/reid/Desktop/DupeFinder/server/node_modules/async/internal/parallel.js:30:5)
at parallelLimit (/Users/reid/Desktop/DupeFinder/server/node_modules/async/parallel.js:88:26)
at utils.promiseOrCallback.cb (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/model.js:2489:5)
at Promise (/Users/reid/Desktop/DupeFinder/server/node_modules/mongoose/lib/utils.js:243:5)
at new Promise (<anonymous>)
message: 'Cast to String failed for value "{ field1: \'8773238968\' }" at path "field1"',
name: 'CastError',
stringValue: '"{ field1: \'8773238968\' }"',
kind: 'String',
value: [Object],
path: 'field1',
reason: [Object] } },
_message: 'numSchema validation failed',
name: 'ValidationError' }
Figured it out.
I console.log(JSON.stringify(data)) and found that the value of this is: "field1" : "field1" : "phone number here"
It had two field ones.
I fixed it by saying: JSON.stringify(data.field1) rather than JSON.stringify(data) though i think it can be fixed multiple ways.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.