POST rquest [React Native,Node]中的“ JSON Parse错误:意外标识符”

我正在尝试使用后端的react native和node将产品添加到数据库中。 POST请求在使用邮递员时有效,但在尝试从前端发送数据时失败,并显示以下错误:

  

JSON解析错误:意外的标识符“本地”   tryCallOne中的node_modules \ promise \ setimmediate \ core.js:37:14   node_modules \ promise \ setimmediate \ core.js:123:25 in   ...另外8个来自框架内部的堆栈框架

我尝试将请求记录到控制台,然后看到正文为{},这是一个空数组。所以我想身体还没到后端。我该如何解决这个问题?

控制器:

import Locals from './model';

export const createLocals= async (req,res)=> {
    const { imageUrl,title,category,description,address,area,price  }= req.body;
    const newLocals= new Locals({imageUrl,price });
    console.log(req);
try {
    res.status(201).json({locals: await newLocals.save()});
}catch(err){
    return res.status(err.status >= 100 && err.status < 600 ? err.code : 500).send(err.message);
}
}
export const getallLocals= async (req,res)=>{
    try {
        return res.status(200).json({locals: await Locals.find({})});
    }catch(e){
        return res.status(e.status).json({error: true,message: 'Error with Locals'});
    }
}

“原生”中的“添加产品”屏幕:

  const productSubmitHandler = () => {   
   dispatch(productsactions.createProduct(img,prodName,headline,+price));     
  }
  return (
    <View style={styles.container}>
      <View style={styles.midCont}>
        <View style={{alignItems: 'center',padding: 5}}>
        {/* <Image source={this.state.avatarSource} style={styles.uploadAvatar} /> */}
        <TextInput 
            style={{borderRadius: 10,borderWidth: 1,height: 100,width: 100,padding: 5}}
            placeholder='Upload Image'
            value={img} onChangeText={text=>setImg(text)}
          />
        </View>
        <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Product Name
          </Text>
          <TextInput

            style={styles.input}
            placeholder="Product Name"
            value={prodName} onChangeText={text=>setProdName(text)}
          />
        </View>
          <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Category
          </Text>
          <TextInput
            style={styles.input}
            placeholder="Category"
            value={category} onChangeText={text=>setCategory(text)}
          />
        </View>
        <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Headline
          </Text>
          <TextInput
            style={styles.input}
            placeholder="Headline"
            value={headline} onChangeText={text=>setHeadline(text)}
          />
        </View>
        <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Address
          </Text>
          <TextInput
            style={styles.input}
            placeholder="Address"
            value={address} onChangeText={text=>setaddress(text)}
          />
        </View>
        <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Area
          </Text>
          <TextInput
            style={styles.input}
            placeholder="Area"
            value={area} onChangeText={text=>setarea(text)}
          />
        </View>
        <View style={styles.viewItem}>
          <Text style={styles.textStyle}>
            Price
          </Text>
          <TextInput
            style={styles.input}
            placeholder="Price"
            value={price} onChangeText={text=>setPrice(text)}
          />
        </View> 
      </View>
      <View style={styles.viewButton}>
        <Button
        style={styles.button}
        title='Add'
        color={Colors.primary}
        onPress={productSubmitHandler}
        /> 
      </View>
    </View>
  );
}

型号:

import mongoose,{Schema} from 'mongoose';

const LocalsSchema = new Schema ({
    imageUrl: {
        type: String,required: true
    },title: {
        type: String,imageUrl: {
    category: {
        type: String,description: {
        type: String,address: {
        type: String,area: {
        type: String,price: {
        type: Number,required: true
    }
    },});
catheone 回答:POST rquest [React Native,Node]中的“ JSON Parse错误:意外标识符”

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3147199.html

大家都在问