我的nodejs项目中有3个文件,
app.js:
var fs = require('fs');
var dotenv = require('dotenv');
dotenv.load();
var designtokenfile = require ('./designtokenfile.js');
var designtokendb = require ('./designtokendb.js');
var TYPE=process.env.TYPE;
module.exports=
{
storeDesign: function(TYPE) {
if (TYPE == 'file') {
return designtokenfile;
}
else if (TYPE == 'db') {
return designtokendb;
}
}
};
DesignFactory.js:
var fs = require('fs');
var fsstore = {
save : function() {
fs.writeFile('output.txt');
},
load : function() {
var data = fs.readFileSync('edit0.json','utf8');
return data;
}
};
module.exports.designtokenfile=fsstore;
designtokenfile.js:
let postpopFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "PopularPosts")
postpopFetch.predicate = NSPredicate(format: "id = %@", postId)
let resultp = try? CoreDataManager.sharedInstance.managedObjectContext.fetch(postpopFetch)
let resultDatap = resultp as! [PopularPosts]
for object in resultDatap {
print(object.numberOfHearts)
if like {
object.numberOfHearts = object.numberOfHearts + 1
object.isLiked = true
}else{
(object.numberOfHearts > 0) ? (object.numberOfHearts = object.numberOfHearts - 1) : (object.numberOfHearts = 0)
object.isLiked = false
}
}
do {
try CoreDataManager.sharedInstance.managedObjectContext.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
我是nodejs的新手,当我们给/ Tokens它给出错误加载没有定义或store.load不是一个函数。 DesignFactory中的storeDesign方法应该返回一个对象,我需要在designtokenfile中调用load方法。但它没有发生在我的代码中。请帮我迷路。 (假设该文件是函数调用storeDesign中ex的值)
答案 0 :(得分:0)
看看这段代码:
app.get('/Tokens', function(req, res) {
var store=DesignFactory.storeDesign(ex);
console.log(store);
var data=store.load();
res.end(data);
});
现在在之后定义了这个闭包(使用变量ex
),你实际上定义变量ex
:
var ex=process.env.TYPE;
console.log(ex);
所以这个变量不在你的闭包范围内。在闭包定义之前移动变量定义,如下所示:
var ex=process.env.TYPE;
console.log(ex);
app.get('/Tokens', function(req, res) {
var store=DesignFactory.storeDesign(ex);
console.log(store);
var data=store.load();
res.end(data);
});