ValidationError:RFDeviceId:路径“ RFDeviceId”是必需的

时间:2019-12-11 10:48:48

标签: javascript node.js mongodb mongoose

我是新来的,并且只知道nodejs和mongodb的baisc,所以请帮助我学习。

我收到此错误

创建数据集时出错:ValidationError:RFDeviceId:需要路径RFDeviceId。坐标:需要路径coordinates。CoordinatesTime:需要路径CoordinatesTime。速度:Path { {1}}是必需的,sat_tracked:路径是speed。gps_fix_type:路径是sat_tracked。温度:路径是gps_fix_type,湿度:路径{{1 }}是必需的。,bat_v:路径temperatureata是必需的。bat_p:路径humidity是必需的。

我在模型中的datasets.js就是这样

bat_v

和路线中的我的datasets.js

bat_p

如果我设置了必填项:false owner_name期望,然后发送var mongoose = require("mongoose"); // Declare schema var datasetSchema = new mongoose.Schema({ owner_name: {type: String, required: true}, RFDeviceId: {type: String, required: true, index: {unique: true}}, coordinates: {type: Number, required: true}, CoordinatesTime: {type: String, required: true}, speed: {type: String, required: true}, sat_tracked: {type: Number, required: true}, gps_fix_type: {type: Number, required: true}, temperatureata: {type: Number, required: true}, humidity: {type: Number, required: true}, bat_v: {type: Number, required: true}, bat_p: {type: Number, required: true} }); // Export schema mongoose.model("Dataset", datasetSchema);``` 的第一个数据发送时间

输出屏幕截图https://ibb.co/pyVXD01

请为所有人提供帮助。

1 个答案:

答案 0 :(得分:0)

在创建模型所需的所有字段时。因此,无论何时创建任何数据集,都需要提供所有必填字段的数据。

然后在创建数据集查询API中,首先直接将req.body。[filed-value]分配给新变量,该新变量还将复制该原始变量的引用。

当您删除原始变量时,复制的变量也会丢失其值并设置为undefined。read ES Documentation 现在,您的创建查询如下所示:

// POST new dataset request
router.post("/", helper.authenticate, function(req, res) {
    // Used to set the dataset owner
    var sessionUser = req.session.user.name;
    // Get values from the post request
    var RFDeviceId = req.body.RFDeviceId;
    var coordinates = req.body.coordinates;
    var CoordinatesTime = req.body.CoordinatesTime;
    var speed = req.body.speed;
    var sat_tracked = req.body.sat_tracked;
    var gps_fix_type = req.body.gps_fix_type;
    var temperatureata = req.body.temperatureata;
    var humidity = req.body.humidity;
    var bat_v = req.body.bat_v;
    var bat_p = req.body.bat_p;

    // This is so that we can loop through the object in reverse order
    // We do that so that the fields are saved in the right order on the db
    // (this way it will appear in the right order on the 'edit' view)
    var propertiesList = [];
    for (var property in req.body) {
        if (req.body.hasOwnProperty(property)) {
            propertiesList.push(property);
        }
    }
    propertiesList.reverse();

    var variablesFields = {};
    for (var i in propertiesList) {
        console.log(propertiesList[i])
        variablesFields[propertiesList[i]] = {name:req.body[propertiesList[i]],
                                    values: Array}; 
    }

    // Create dataset 
    Dataset.create({
        RFDeviceId: RFDeviceId,
        coordinates: coordinates, 
        CoordinatesTime: CoordinatesTime,
        speed: speed,
        sat_tracked: sat_tracked,
        gps_fix_type: gps_fix_type,
        temperatureata: temperatureata,
        humidity: humidity,
        bat_v: bat_v,
        bat_p: bat_p,
        owner_name: sessionUser
    }, function(err, dataset) {
        if (err) {
            console.log("Error creating the dataset: " + err);
            req.session.error = "A problem occured when creating the dataset. Please try again.";
        } else {
            console.log("New dataset created with id: " + dataset._id);
            req.session.success = "Dataset " + "name" + " created successfully.";
        }
        res.redirect("/index");
    });
});

我希望你有做正确的主意。

[如果对您有帮助,请投票回答此问题]