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

时间:2019-11-07 05:58:38

标签: node.js reactjs react-native express

我正在尝试使用后端的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, title, category, description, address, area, 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,category,headline,address,area,+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,
        required: true
    },
     imageUrl: {
    category: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    address: {
        type: String,
        required: true
    },
    area: {
        type: String,
        required: true
    },
    price: {
        type: Number,
        required: true
    }
    },

});

0 个答案:

没有答案