javascript - Cast to string failed for value - Stack Overflow

I have this problem I'm tiredI can't solve this problem Cast to string failed for value &q

I have this problem I'm tired I can't solve this problem Cast to string failed for value \"{}\" at path \"post\

{
  "confirmation": "fail",
  "message": {
    "message": "Cast to string failed for value \"{}\" at path \"post\"",
    "name": "CastError",
    "stringValue": "\"{}\"",
    "kind": "string",
    "value": {

    },
    "path": "post"
  }
}

hope you will help me

index.html

    <h1>CreateComment</h1>
    <form action="/api/ment" method="post">
      <input type="text" name="post" placeholder="Post" /><br />
      <input type="text" name="text" placeholder="Text"><br />
      <input type="submit" value"Create" />
    </form>

Comment.js

var mongoose = require('mongoose')

var CommentSchema = new mongoose.Schema({
  profile: {type: mongoose.Schema.Types.Mixed, default: {}},
  post: {type: String, default:{}},
  text: {type: String, default: ''},
  timestamp: {type:Date, default:Date.now}
})

module.exports = mongoose.model('CommentSchema', CommentSchema);

controllers.js

var Comment = require('../models/Comment')
var promise = require('bluebird')

module.exports = {

  post: function(params, isRaw) {
    return new Promise(function(resolve, reject) {
      Comment.create(params, function(err, ment) {
        if (err) {
          reject(err)
          return
        }
        console.log(ment)
        resolve(ment)
      })
    })
  }

}

api.js

var express = require('express');
var router = express.Router();
var controllers = require('../controllers');


router.post('/:resource', function(req, res, next) {
  var resource = req.params.resource;
  var controller = controllers[resource];

  if (controller == null) {
    res.json({
      confirmation: 'fail',
      message: 'Invalid Resource'
    })

    return
  }

  controller.post(req.body, false)
    .then(function(result) {
      res.json({
        confirmation: 'success',
        result: result
      })
    })
    .catch(function(err) {
      res.json({
        confirmation: 'fail',
        message: err
      })
    })
})

module.exports = router;

this all of my codes

I have this problem I'm tired I can't solve this problem Cast to string failed for value \"{}\" at path \"post\

{
  "confirmation": "fail",
  "message": {
    "message": "Cast to string failed for value \"{}\" at path \"post\"",
    "name": "CastError",
    "stringValue": "\"{}\"",
    "kind": "string",
    "value": {

    },
    "path": "post"
  }
}

hope you will help me

index.html

    <h1>CreateComment</h1>
    <form action="/api/ment" method="post">
      <input type="text" name="post" placeholder="Post" /><br />
      <input type="text" name="text" placeholder="Text"><br />
      <input type="submit" value"Create" />
    </form>

Comment.js

var mongoose = require('mongoose')

var CommentSchema = new mongoose.Schema({
  profile: {type: mongoose.Schema.Types.Mixed, default: {}},
  post: {type: String, default:{}},
  text: {type: String, default: ''},
  timestamp: {type:Date, default:Date.now}
})

module.exports = mongoose.model('CommentSchema', CommentSchema);

controllers.js

var Comment = require('../models/Comment')
var promise = require('bluebird')

module.exports = {

  post: function(params, isRaw) {
    return new Promise(function(resolve, reject) {
      Comment.create(params, function(err, ment) {
        if (err) {
          reject(err)
          return
        }
        console.log(ment)
        resolve(ment)
      })
    })
  }

}

api.js

var express = require('express');
var router = express.Router();
var controllers = require('../controllers');


router.post('/:resource', function(req, res, next) {
  var resource = req.params.resource;
  var controller = controllers[resource];

  if (controller == null) {
    res.json({
      confirmation: 'fail',
      message: 'Invalid Resource'
    })

    return
  }

  controller.post(req.body, false)
    .then(function(result) {
      res.json({
        confirmation: 'success',
        result: result
      })
    })
    .catch(function(err) {
      res.json({
        confirmation: 'fail',
        message: err
      })
    })
})

module.exports = router;

this all of my codes

Share Improve this question edited Jan 16, 2017 at 14:55 MEAbid asked Jan 16, 2017 at 14:52 MEAbidMEAbid 5507 silver badges12 bronze badges 2
  • Can you do a little debugging first and e to us with an actual question? Maybe show what you've tried? – evolutionxbox Commented Jan 16, 2017 at 14:54
  • expecting type is string for which you got response as object. – Jyothi Babu Araja Commented Jan 16, 2017 at 14:56
Add a ment  | 

2 Answers 2

Reset to default 5

I think that {} is not a valid default for the 'post' member.

post: {type: String, default:{}},

Either the 'post' default value should be a string and you need to provide one, or an empty object, and then the type should not be "String".

Take a look at my response in this other related question. The same applies for 'reserved mongoose words' such as 'type' and 'value':

https://stackoverflow./a/72392997/9936281

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744345608a4569664.html

相关推荐

  • javascript - Cast to string failed for value - Stack Overflow

    I have this problem I'm tiredI can't solve this problem Cast to string failed for value &q

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信