我有这个代码,我试图在LocalHost上运行它。我一直收到意外的令牌错误。有谁知道为什么?
我要做的是创建一个简单的API,从用户那里获取输入并将它们保存到monogo数据库中。
var PodDoc = require('../models/pods.js');
module.exports = {
save: save
}
function save(pod, callback){
var podToSave = new PodDoc();
podToSave.firstName = pod.firstName;
podToSave.lastName = pod.lastName;
podToSave.skills = pod.skills;
podToSave.avatarUrl = pod.avatarUrl;
podToSave.address = {
address.number = pod.address.number; //This is where the Unexpected Token is
address.lineOne = pod.address.lineOne;
address.lineTwo = pod.address.lineTwo;
address.postcode = pod.address.postcode;
};
podToSave.phoneNumbers = {
podToSave.phoneNumbers.mobile = pod.phoneNumbers.mobile;
podToSave.phoneNumbers.landline = pod.phoneNumbers.landline;
}
podToSave.save(function(err){
if(err){
console.log(err);
} else {
console.log("Cool!");
callback();
}
})
}
答案 0 :(得分:6)
对象属性以这种方式设置:
var obj = {
foo: "foo", //':' instead of '=' and ',' not ';'
bar: "bar"
};
如果您想稍后设置一个属性:
obj.test = "test";
obj["test"] = "test";
var test = "test";
obj[test] = test; //Using a variable value as property
但不喜欢这样:
var obj = {
obj.foo: "foo" //syntax error
};
这是你修改的代码:
podToSave.address = {
//number not address.number
number: pod.address.number, //= and ; changed
lineOne: pod.address.lineOne,
lineTwo: pod.address.lineTwo,
postcode: pod.address.postcode
};
podToSave.phoneNumbers = {
mobile: pod.phoneNumbers.mobile,
landline: pod.phoneNumbers.landline
}