Envelope:无法读取sails.js中未定义的属性"id"每当我有发送创建请求时,他们都会给我上面的错误,型号:Car.js
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
unique: true
},
engine: {
type: 'String',
},
type: {
type: 'String'
},
colors: {
collection: 'color',
via: 'car'
}
},
};
型号:Color.js
module.exports = {
attributes: {
color: {
type: 'String',
unique: true,
required: true
},
car: {
model: 'car',
required: true
}
},
};
CarController.js
请尽快帮我bcz我有业余学习者在sails.js
create: async function (req, res) {
let name = req.param('name');
let engine = req.param('engine');
let type = req.param('type');
let colorName = req.param('color');
if(!name){
res.badRequest({err : 'invalid Name'});
}
if(!engine){
res.badRequest({err : 'invalid Engine'});
}
if(!type){
res.badRequest({err : 'invalid Type'});
}
if(!colorName){
res.badRequest({err : 'invalid Color Name'});
}
await Car.create({
name: name,
engine: engine,
type: type
})
.exec((err, newcar) => {
if(err) {
return res.serverError(err);
}
//Create New Color
Color.create({
color: colorName,
car: newcar.id,
})
.exec((err, _color) => {
if(err) {
return res.serverError(err);
}
})
res.send(newcar, _color);
})
},
这看起来像id错误,请尽快解决
在sails.js 中工作的查找
find: async(req, res) => {
try{
let doc = await db.collection('color').aggregate([
{
$lookup: {
from: 'car',
localField: 'carID',
foreignField: 'carID',
as: 'Car'
}
}
]).toArray();
res.ok(doc);
} catch (error) {
return res.badRequest(error);
}
}